2019.12.02 Update: Thanks for digging friendsSwteefishThe official Chinese name of the alert propertyWrapperAttribute packaging
— Here is the body —
What is the @ propertyWrapper?
The Property Wrapper is taken literallyAttribute wrapper
(MY second grade English level hard translation, write when there seems to be no unified call. Have the classmate that knows scientific name trouble remind me to thank ~).
It’sFunction object
isattribute
itskeynote
That is: passproperty Wrapper
Mechanism, right, something like thatattribute
The implementation code to do the same package.
To put it simply: duplicate or similar code can be removed using @propertyWrapper.
PropertyWrapper is not that hard, it’s new. Just copy the following Demo and run it through the playGround.
To give you an idea, let’s save an example of whether or not to display a novice boot in UserDefaults
Without @propertyWrapper. 😔
However, there are a lot of places in your project where you need UserDefaults to hold local data, and when you have a lot of data, you get a lot of repetitive code.
When there is @propertyWrapper. 😁
I have written the specific usage and knowledge of @propertywrapper in the demo. Running in the PlayGround is very steady.
When adding a new key to store data, just add the new attribute and wrapper to UserDefaultsConfig
Over.
Attached: Demo code
extension UserDefaults { public enum Keys { static let hadShownGuideView = "had_shown_guide_view" } var hadShownGuideView: Bool { set { set(newValue, forKey: Keys.hadShownGuideView) } get { return bool(forKey: Keys.hadshownguideview)}}} // let hadShownGuide = UserDefaults.standard.hadShownGuideView if ! HadShownGuide {/// displays the newbie boot and saves it locally as showGuideView() is displayed. UserDefaults.standard.hadShownGuideView = true }Copy the code
Struct UserDefault<T> {/// where the properties key and defaultValue are Let key: String let defaultValue: T init(_ key: String, defaultValue: T) {self.key = key self.defaultValue = defaultValue} // wrappedValue is a property that @propertyWrapper must implement The get method actually goes all the way to wrappedValue's set get method. var wrappedValue: T { get { return UserDefaults.standard.object(forKey: key) as? T ?? defaultValue } set { UserDefaults.standard.set(newValue, forKey: Struct UserDefaultsConfig {// tell the compiler that I want to wrap hadShownGuideView. Some keys and default values of the hadShownGuideView attribute are already implemented in the constructor of the UserDefault wrapper @userdefault ("had_shown_guide_view", defaultValue: false) static var hadShownGuideView: Bool} UserDefaultsConfig.hadShownGuideView = false print(UserDefaultsConfig.hadShownGuideView) // false UserDefaultsConfig.hadShownGuideView = true print(UserDefaultsConfig.hadShownGuideView) // trueCopy the code
struct UserDefaultsConfig { @UserDefault("had_shown_guide_view", defaultValue: false) static var hadShownGuideView: @userdefault ("username", defaultValue: "unknown") static var username: String}Copy the code
References:
Earliest proposal for Property Wrapper: Swift-Evolution 0258 Property wrappers to remove boilerplate code in Swift nshipster-propertywrapper