KissDefault icon indicating copy to clipboard operation
KissDefault copied to clipboard

[EXPERIMANTAL] 💋Keep It(UserDefaults propertyWrapper) Simple and Stupid

CAUTION: This lib IS NOT production ready yet

💋 KissDefault

Language Carthage compatible

Keep It(UserDefaults propertyWrapper) Simple and Stupid

KissDefault is a polished propertyWrapper which minimize the boilerplate of writing property keys. If you have no idea of what propertyWrapper is, please head to Property wrappers in Swift.

The old redundant way

// You have to define every key name by yourself, seriously?
@UserDefaultsBacked(key: "signature")
var messageSignature: String?

@UserDefaultsBacked(key: "mark-as-read", defaultValue: true)
var autoMarkMessagesAsRead: Bool

@UserDefaultsBacked(key: "search-page-size", defaultValue: 20)
var numberOfSearchResultsPerPage: Int

With KissDefault

@Kiss()
var messageSignature: String?

@Kiss(default: true)
var autoMarkMessagesAsRead: Bool

@Kiss(default: 20)
var numberOfSearchResultsPerPage: Int

You can even benefit from Codable protocol

struct ObjStruct: Equatable, Codable {
    var wrappedValue: Int = 1024

    static func == (lhs: Self, rhs: Self) -> Bool {
        lhs.wrappedValue == rhs.wrappedValue
    }
}
...
@Kiss()
static var objV1: ObjStruct?

How it works

KissDefault extracts the current callstack in wrappedValue (with Thread.callStackSymbols), demangles the presented variable name and use it as the key to store values. This process is done by a simple regex which may not be a performance concern. Please read this article if you're interested in demangling in Swift.

Todos

  • [ ] Find a way to get variable name without symbolicating
  • [ ] CocoaPods
  • [ ] Customized key
  • [ ] Boost demangle performance

License

This code is distributed under the terms and conditions of the MIT license.