{"id":410557,"date":"2024-06-29T21:27:34","date_gmt":"2024-06-29T21:27:34","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=410557"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=410557","title":{"rendered":"<span>Swift. UserDefaults. Property wrapper<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/957\/467\/86f\/95746786f589514902338ddffc857006.png\" alt=\"Xcode\" title=\"Xcode\" width=\"1280\" height=\"720\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/957\/467\/86f\/95746786f589514902338ddffc857006.png\"\/><figcaption>Xcode<\/figcaption><\/figure>\n<p>\u0412\u0440\u044f\u0434 \u043b\u0438 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0445\u043e\u0442\u044c \u043e\u0434\u043d\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043a \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. \u041e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0434\u043b\u044f \u044d\u0442\u0438\u0445 \u0446\u0435\u043b\u0435\u0439 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043c\u0430\u0441\u0441\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u0439, \u0438 \u043a\u0430\u0436\u0434\u043e\u0435 \u0438\u0437 \u043d\u0438\u0445 \u0440\u0435\u0448\u0430\u0435\u0442 \u043e\u0434\u043d\u0443, \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443. \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0440\u0435\u0447\u044c \u043f\u043e\u0439\u0434\u0435\u0442 \u043e \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e\u043c \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 UserDefaults \u0438 \u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<h2>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/h2>\n<p>\u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0432 \u043f\u043e\u0438\u0441\u043a \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043d\u0430 \u043f\u0440\u043e\u0441\u0442\u043e\u0440\u0430\u0445 \u0441\u0435\u0442\u0438 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442 \u0431\u044b\u043b\u0438 \u043d\u0430\u0439\u0434\u0435\u043d\u044b \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u044b:<\/p>\n<ol>\n<li>\n<p><a href=\"https:\/\/www.swiftbysundell.com\/articles\/property-wrappers-in-swift\/\" rel=\"noopener noreferrer nofollow\">Property wrappers in Swift<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/swiftsenpai.com\/swift\/create-the-perfect-userdefaults-wrapper-using-property-wrapper\/\" rel=\"noopener noreferrer nofollow\">Create the Perfect UserDefaults Wrapper Using Property Wrapper<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/www.jessesquires.com\/blog\/2021\/03\/26\/a-better-approach-to-writing-a-userdefaults-property-wrapper\/\" rel=\"noopener noreferrer nofollow\">A better approach to writing a UserDefaults Property Wrapper<\/a><\/p>\n<\/li>\n<\/ol>\n<p>\u041e\u0434\u043d\u0430\u043a\u043e \u0432\u0441\u0435 \u043e\u043d\u0438 \u0438\u043c\u0435\u044e\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u043e\u0432:<\/p>\n<ol>\n<li>\n<p>\u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u043c, \u0432\u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u0435 \u0447\u0435\u0433\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e-\u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e<\/p>\n<\/li>\n<li>\n<p>\u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435\u043c \u0438\u043b\u0438 OptionSet<\/p>\n<\/li>\n<li>\n<p>\u0412\u0441\u0435 \u043e\u043d\u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u043d\u0435\u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u043c\u044b\u0435 \u0442\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u0432\u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u0435 \u0447\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0430\u0432\u0430\u0440\u0438\u0439\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442 \u0441\u0432\u043e\u044e \u0440\u0430\u0431\u043e\u0442\u0443<\/p>\n<\/li>\n<\/ol>\n<p>\u041d\u043e \u0435\u0441\u043b\u0438 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c\u0441\u044f \u043a \u043f\u0435\u0440\u0432\u043e\u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u0443 \u0438 \u0438\u0437\u0443\u0447\u0438\u0442\u044c <a href=\"https:\/\/developer.apple.com\/documentation\/swiftui\/appstorage\" rel=\"noopener noreferrer nofollow\">AppStorage<\/a> \u043e\u0442 <a href=\"https:\/\/developer.apple.com\" rel=\"noopener noreferrer nofollow\">Apple<\/a>, \u0442\u043e \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u043a, \u043f\u0440\u0438\u0441\u0443\u0449\u0438\u0439 \u0443\u0436\u0435 \u043a \u0434\u0430\u043d\u043d\u043e\u043c\u0443 \u0440\u0435\u0448\u0435\u043d\u0438\u044e \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f <strong>iOS 14<\/strong>, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c \u043e\u0442 framework <strong>SwiftUI<\/strong>.<\/p>\n<p>\u041d\u0438\u0436\u0435 \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f propertyWrapper UserDefault, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043b\u0438\u0448\u0435\u043d \u0432\u0441\u0435\u0445 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0445 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u043e\u0432, \u0432 \u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u0430\u043a \u0440\u0435\u0448\u0430\u0435\u0442 \u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443.<\/p>\n<pre><code class=\"swift\">import SwiftUI  @propertyWrapper public struct UserDefault&lt;Value>: DynamicProperty { private let get: () -> Value private let set: (Value) -> Void  public var wrappedValue: Value { get { get() } nonmutating set { set(newValue) } } }  public extension UserDefault { init(wrappedValue: Value, _ key: String, store: UserDefaults = .standard) where Value == Bool { self.init(defaultValue: wrappedValue, key: key, store: store) }  init(wrappedValue: Value, _ key: String, store: UserDefaults = .standard) where Value == Int { self.init(defaultValue: wrappedValue, key: key, store: store) }  init(wrappedValue: Value, _ key: String, store: UserDefaults = .standard) where Value == Double { self.init(defaultValue: wrappedValue, key: key, store: store) }  init(wrappedValue: Value, _ key: String, store: UserDefaults = .standard) where Value == String { self.init(defaultValue: wrappedValue, key: key, store: store) }  init(wrappedValue: Value, _ key: String, store: UserDefaults = .standard) where Value == URL { self.init(defaultValue: wrappedValue, key: key, store: store) }  init(wrappedValue: Value, _ key: String, store: UserDefaults = .standard) where Value == Data { self.init(defaultValue: wrappedValue, key: key, store: store) }  private init(defaultValue: Value, key: String, store: UserDefaults) { get = { let value = store.value(forKey: key) as? Value return value ?? defaultValue }  set = { newValue in store.set(newValue, forKey: key) } } }  public extension UserDefault where Value: ExpressibleByNilLiteral { init(_ key: String, store: UserDefaults = .standard) where Value == Bool? { self.init(wrappedType: Bool.self, key: key, store: store) }  init(_ key: String, store: UserDefaults = .standard) where Value == Int? { self.init(wrappedType: Int.self, key: key, store: store) }  init(_ key: String, store: UserDefaults = .standard) where Value == Double? { self.init(wrappedType: Double.self, key: key, store: store) }  init(_ key: String, store: UserDefaults = .standard) where Value == String? { self.init(wrappedType: String.self, key: key, store: store) }  init(_ key: String, store: UserDefaults = .standard) where Value == URL? { self.init(wrappedType: URL.self, key: key, store: store) }  init(_ key: String, store: UserDefaults = .standard) where Value == Data? { self.init(wrappedType: Data.self, key: key, store: store) }  private init&lt;T>(wrappedType: T.Type, key: String, store: UserDefaults) { get = { let value = store.value(forKey: key) as? Value return value ?? nil }  set = { newValue in let newValue = newValue as? Optional&lt;T>  if let newValue { store.set(newValue, forKey: key) } else { store.removeObject(forKey: key) } } } }  public extension UserDefault where Value: RawRepresentable { init(wrappedValue: Value, _ key: String, store: UserDefaults = .standard) where Value.RawValue == String { self.init(defaultValue: wrappedValue, key: key, store: store) }  init(wrappedValue: Value, _ key: String, store: UserDefaults = .standard) where Value.RawValue == Int { self.init(defaultValue: wrappedValue, key: key, store: store) }  private init(defaultValue: Value, key: String, store: UserDefaults) { get = { var value: Value?  if let rawValue = store.value(forKey: key) as? Value.RawValue { value = Value(rawValue: rawValue) }  return value ?? defaultValue }  set = { newValue in let value = newValue.rawValue store.set(value, forKey: key) } } }  public extension UserDefault { init&lt;R>(_ key: String, store: UserDefaults = .standard) where Value == R?, R: RawRepresentable, R.RawValue == Int { self.init(key: key, store: store) }  init&lt;R>(_ key: String, store: UserDefaults = .standard) where Value == R?, R: RawRepresentable, R.RawValue == String { self.init(key: key, store: store) }  private init&lt;R>(key: String, store: UserDefaults) where Value == R?, R: RawRepresentable { get = { if let rawValue = store.value(forKey: key) as? R.RawValue { return R(rawValue: rawValue) } else { return nil } }   set = { newValue in let newValue = newValue as Optional&lt;R>  if let newValue { store.set(newValue.rawValue, forKey: key) } else { store.removeObject(forKey: key) } } } } <\/code><\/pre>\n<p>\u0421\u0442\u043e\u0438\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 propertyWrapper \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0443\u0434\u043e\u0431\u043d\u0435\u0439, \u0442\u0430\u043a \u043a\u0430\u043a \u043a\u043e\u0434\u0430 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043c\u0435\u043d\u044c\u0448\u0435, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0438 \u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f \u043e\u043d \u043b\u0435\u0433\u0447\u0435.<\/p>\n<pre><code class=\"swift\">final class UserSettings { @UserDefault(\"age\") var age: Int?  @UserDefault(\"name\") var name: String?  @UserDefault(\"planet\") var planet: Planet = .earth  func clear() { age = nil name = nil planet = .earth } }<\/code><\/pre>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0412 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u0432\u0440\u0435\u043c\u044f, \u0441 \u043c\u043e\u043c\u0435\u043d\u0442\u0430 \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f SwiftUI, \u0432\u0441\u0435 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u043e\u0441\u0442\u044c\u044e \u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f <a class=\"mention\" href=\"\/users\/propertywrapper\">@propertyWrapper<\/a>. \u041e\u0434\u043d\u0430\u043a\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0442\u0430\u043a\u0438\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043d\u0430\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u0442 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f. \u041a \u043f\u0440\u0438\u043c\u0435\u0440\u0443, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u0434\u043d\u0438\u043c\u0430\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e iOS \u0438 \u0432\u043d\u0435\u0434\u0440\u044f\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c \u043e\u0442 framework SwiftUI. \u041d\u043e \u044d\u0442\u043e \u0432\u043e\u0432\u0441\u0435 \u043d\u0435 \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0442\u0435\u043c, \u0447\u0442\u043e \u0435\u0441\u0442\u044c, \u0438 \u043f\u0440\u0438\u043c\u0435\u0440 \u0442\u043e\u043c\u0443 \u0431\u044b\u043b \u043e\u043f\u0438\u0441\u0430\u043d \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435.<\/p>\n<ol>\n<li>\n<p><a href=\"https:\/\/acerodstin.ru\/ru-ru\/articles\/%D1%80%D0%B0%D0%B7%D1%80%D0%B0%D0%B1%D0%BE%D1%82%D0%BA%D0%B0-propertywrapper-%D0%B4%D0%BB%D1%8F-userdefaults\" rel=\"noopener noreferrer nofollow\">\u041e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/userdefaults\" rel=\"noopener noreferrer nofollow\">UserDefaults<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/developer.apple.com\/documentation\/swiftui\/appstorage\" rel=\"noopener noreferrer nofollow\">AppStorage<\/a><\/p>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/670378\/\"> https:\/\/habr.com\/ru\/articles\/670378\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><figcaption>Xcode<\/figcaption><\/figure>\n<p>\u0412\u0440\u044f\u0434 \u043b\u0438 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0445\u043e\u0442\u044c \u043e\u0434\u043d\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043a \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. \u041e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0434\u043b\u044f \u044d\u0442\u0438\u0445 \u0446\u0435\u043b\u0435\u0439 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043c\u0430\u0441\u0441\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u0439, \u0438 \u043a\u0430\u0436\u0434\u043e\u0435 \u0438\u0437 \u043d\u0438\u0445 \u0440\u0435\u0448\u0430\u0435\u0442 \u043e\u0434\u043d\u0443, \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443. \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0440\u0435\u0447\u044c \u043f\u043e\u0439\u0434\u0435\u0442 \u043e \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e\u043c \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 UserDefaults \u0438 \u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<h2>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/h2>\n<p>\u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0432 \u043f\u043e\u0438\u0441\u043a \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043d\u0430 \u043f\u0440\u043e\u0441\u0442\u043e\u0440\u0430\u0445 \u0441\u0435\u0442\u0438 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442 \u0431\u044b\u043b\u0438 \u043d\u0430\u0439\u0434\u0435\u043d\u044b \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u044b:<\/p>\n<ol>\n<li>\n<p><a href=\"https:\/\/www.swiftbysundell.com\/articles\/property-wrappers-in-swift\/\" rel=\"noopener noreferrer nofollow\">Property wrappers in Swift<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/swiftsenpai.com\/swift\/create-the-perfect-userdefaults-wrapper-using-property-wrapper\/\" rel=\"noopener noreferrer nofollow\">Create the Perfect UserDefaults Wrapper Using Property Wrapper<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/www.jessesquires.com\/blog\/2021\/03\/26\/a-better-approach-to-writing-a-userdefaults-property-wrapper\/\" rel=\"noopener noreferrer nofollow\">A better approach to writing a UserDefaults Property Wrapper<\/a><\/p>\n<\/li>\n<\/ol>\n<p>\u041e\u0434\u043d\u0430\u043a\u043e \u0432\u0441\u0435 \u043e\u043d\u0438 \u0438\u043c\u0435\u044e\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u043e\u0432:<\/p>\n<ol>\n<li>\n<p>\u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u043c, \u0432\u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u0435 \u0447\u0435\u0433\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e-\u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e<\/p>\n<\/li>\n<li>\n<p>\u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435\u043c \u0438\u043b\u0438 OptionSet<\/p>\n<\/li>\n<li>\n<p>\u0412\u0441\u0435 \u043e\u043d\u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u043d\u0435\u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u043c\u044b\u0435 \u0442\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u0432\u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u0435 \u0447\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0430\u0432\u0430\u0440\u0438\u0439\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442 \u0441\u0432\u043e\u044e \u0440\u0430\u0431\u043e\u0442\u0443<\/p>\n<\/li>\n<\/ol>\n<p>\u041d\u043e \u0435\u0441\u043b\u0438 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c\u0441\u044f \u043a \u043f\u0435\u0440\u0432\u043e\u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u0443 \u0438 \u0438\u0437\u0443\u0447\u0438\u0442\u044c <a href=\"https:\/\/developer.apple.com\/documentation\/swiftui\/appstorage\" rel=\"noopener noreferrer nofollow\">AppStorage<\/a> \u043e\u0442 <a href=\"https:\/\/developer.apple.com\" rel=\"noopener noreferrer nofollow\">Apple<\/a>, \u0442\u043e \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u043a, \u043f\u0440\u0438\u0441\u0443\u0449\u0438\u0439 \u0443\u0436\u0435 \u043a \u0434\u0430\u043d\u043d\u043e\u043c\u0443 \u0440\u0435\u0448\u0435\u043d\u0438\u044e \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f <strong>iOS 14<\/strong>, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c \u043e\u0442 framework <strong>SwiftUI<\/strong>.<\/p>\n<p>\u041d\u0438\u0436\u0435 \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f propertyWrapper UserDefault, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043b\u0438\u0448\u0435\u043d \u0432\u0441\u0435\u0445 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0445 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u043e\u0432, \u0432 \u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u0430\u043a \u0440\u0435\u0448\u0430\u0435\u0442 \u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443.<\/p>\n<pre><code class=\"swift\">import SwiftUI  @propertyWrapper public struct UserDefault&lt;Value>: DynamicProperty { private let get: () -> Value private let set: (Value) -> Void  public var wrappedValue: Value { get { get() } nonmutating set { set(newValue) } } }  public extension UserDefault { init(wrappedValue: Value, _ key: String, store: UserDefaults = .standard) where Value == Bool { self.init(defaultValue: wrappedValue, key: key, store: store) }  init(wrappedValue: Value, _ key: String, store: UserDefaults = .standard) where Value == Int { self.init(defaultValue: wrappedValue, key: key, store: store) }  init(wrappedValue: Value, _ key: String, store: UserDefaults = .standard) where Value == Double { self.init(defaultValue: wrappedValue, key: key, store: store) }  init(wrappedValue: Value, _ key: String, store: UserDefaults = .standard) where Value == String { self.init(defaultValue: wrappedValue, key: key, store: store) }  init(wrappedValue: Value, _ key: String, store: UserDefaults = .standard) where Value == URL { self.init(defaultValue: wrappedValue, key: key, store: store) }  init(wrappedValue: Value, _ key: String, store: UserDefaults = .standard) where Value == Data { self.init(defaultValue: wrappedValue, key: key, store: store) }  private init(defaultValue: Value, key: String, store: UserDefaults) { get = { let value = store.value(forKey: key) as? Value return value ?? defaultValue }  set = { newValue in store.set(newValue, forKey: key) } } }  public extension UserDefault where Value: ExpressibleByNilLiteral { init(_ key: String, store: UserDefaults = .standard) where Value == Bool? { self.init(wrappedType: Bool.self, key: key, store: store) }  init(_ key: String, store: UserDefaults = .standard) where Value == Int? { self.init(wrappedType: Int.self, key: key, store: store) }  init(_ key: String, store: UserDefaults = .standard) where Value == Double? { self.init(wrappedType: Double.self, key: key, store: store) }  init(_ key: String, store: UserDefaults = .standard) where Value == String? { self.init(wrappedType: String.self, key: key, store: store) }  init(_ key: String, store: UserDefaults = .standard) where Value == URL? { self.init(wrappedType: URL.self, key: key, store: store) }  init(_ key: String, store: UserDefaults = .standard) where Value == Data? { self.init(wrappedType: Data.self, key: key, store: store) }  private init&lt;T>(wrappedType: T.Type, key: String, store: UserDefaults) { get = { let value = store.value(forKey: key) as? Value return value ?? nil }  set = { newValue in let newValue = newValue as? Optional&lt;T>  if let newValue { store.set(newValue, forKey: key) } else { store.removeObject(forKey: key) } } } }  public extension UserDefault where Value: RawRepresentable { init(wrappedValue: Value, _ key: String, store: UserDefaults = .standard) where Value.RawValue == String { self.init(defaultValue: wrappedValue, key: key, store: store) }  init(wrappedValue: Value, _ key: String, store: UserDefaults = .standard) where Value.RawValue == Int { self.init(defaultValue: wrappedValue, key: key, store: store) }  private init(defaultValue: Value, key: String, store: UserDefaults) { get = { var value: Value?  if let rawValue = store.value(forKey: key) as? Value.RawValue { value = Value(rawValue: rawValue) }  return value ?? defaultValue }  set = { newValue in let value = newValue.rawValue store.set(value, forKey: key) } } }  public extension UserDefault { init&lt;R>(_ key: String, store: UserDefaults = .standard) where Value == R?, R: RawRepresentable, R.RawValue == Int { self.init(key: key, store: store) }  init&lt;R>(_ key: String, store: UserDefaults = .standard) where Value == R?, R: RawRepresentable, R.RawValue == String { self.init(key: key, store: store) }  private init&lt;R>(key: String, store: UserDefaults) where Value == R?, R: RawRepresentable { get = { if let rawValue = store.value(forKey: key) as? R.RawValue { return R(rawValue: rawValue) } else { return nil } }   set = { newValue in let newValue = newValue as Optional&lt;R>  if let newValue { store.set(newValue.rawValue, forKey: key) } else { store.removeObject(forKey: key) } } } } <\/code><\/pre>\n<p>\u0421\u0442\u043e\u0438\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 propertyWrapper \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0443\u0434\u043e\u0431\u043d\u0435\u0439, \u0442\u0430\u043a \u043a\u0430\u043a \u043a\u043e\u0434\u0430 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043c\u0435\u043d\u044c\u0448\u0435, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0438 \u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f \u043e\u043d \u043b\u0435\u0433\u0447\u0435.<\/p>\n<pre><code class=\"swift\">final class UserSettings { @UserDefault(\"age\") var age: Int?  @UserDefault(\"name\") var name: String?  @UserDefault(\"planet\") var planet: Planet = .earth  func clear() { age = nil name = nil planet = .earth } }<\/code><\/pre>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0412 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u0432\u0440\u0435\u043c\u044f, \u0441 \u043c\u043e\u043c\u0435\u043d\u0442\u0430 \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f SwiftUI, \u0432\u0441\u0435 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u043e\u0441\u0442\u044c\u044e \u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f <a class=\"mention\" href=\"\/users\/propertywrapper\">@propertyWrapper<\/a>. \u041e\u0434\u043d\u0430\u043a\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0442\u0430\u043a\u0438\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043d\u0430\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u0442 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f. \u041a \u043f\u0440\u0438\u043c\u0435\u0440\u0443, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u0434\u043d\u0438\u043c\u0430\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e iOS \u0438 \u0432\u043d\u0435\u0434\u0440\u044f\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c \u043e\u0442 framework SwiftUI. \u041d\u043e \u044d\u0442\u043e \u0432\u043e\u0432\u0441\u0435 \u043d\u0435 \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0442\u0435\u043c, \u0447\u0442\u043e \u0435\u0441\u0442\u044c, \u0438 \u043f\u0440\u0438\u043c\u0435\u0440 \u0442\u043e\u043c\u0443 \u0431\u044b\u043b \u043e\u043f\u0438\u0441\u0430\u043d \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435.<\/p>\n<ol>\n<li>\n<p><a href=\"https:\/\/acerodstin.ru\/ru-ru\/articles\/%D1%80%D0%B0%D0%B7%D1%80%D0%B0%D0%B1%D0%BE%D1%82%D0%BA%D0%B0-propertywrapper-%D0%B4%D0%BB%D1%8F-userdefaults\" rel=\"noopener noreferrer nofollow\">\u041e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/userdefaults\" rel=\"noopener noreferrer nofollow\">UserDefaults<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/developer.apple.com\/documentation\/swiftui\/appstorage\" rel=\"noopener noreferrer nofollow\">AppStorage<\/a><\/p>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/670378\/\"> https:\/\/habr.com\/ru\/articles\/670378\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-410557","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/410557","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=410557"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/410557\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=410557"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=410557"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=410557"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}