6

我正在尝试制作一个ObservableObject具有包装UserDefaults变量的属性。

为了符合ObservableObject,我需要用 包装属性@Published。不幸的是,我不能将其应用于计算属性,就像我用于UserDefaults值一样。

我怎样才能让它工作?我必须做什么才能实现@Published行为?

4

4 回答 4

8

对于现有的 @Published 属性

这是一种方法,您可以创建一个惰性属性,该属性返回从您的发布者派生的@Published发布者:

import Combine

class AppState: ObservableObject {
  @Published var count: Int = 0
  lazy var countTimesTwo: AnyPublisher<Int, Never> = {
    $count.map { $0 * 2 }.eraseToAnyPublisher()
  }()
}

let appState = AppState()
appState.count += 1
appState.$count.sink { print($0) }
appState.countTimesTwo.sink { print($0) }
// => 1
// => 2
appState.count += 1
// => 2
// => 4

然而,这是人为的,可能几乎没有实际用途。有关更有用的内容,请参阅下一节...


对于任何支持 KVO 的对象

UserDefaults支持 KVO。我们可以创建一个通用的解决方案KeyPathObserver,称为响应对支持 KVO 的 Object 的更改,并使用单个@ObjectObserver. 以下示例将在 Playground 中运行:

import Foundation
import UIKit
import PlaygroundSupport
import SwiftUI
import Combine

let defaults = UserDefaults.standard

extension UserDefaults {
  @objc var myCount: Int {
    return integer(forKey: "myCount")
  }

  var myCountSquared: Int {
    return myCount * myCount
  }
}

class KeyPathObserver<T: NSObject, V>: ObservableObject {
  @Published var value: V
  private var cancel = Set<AnyCancellable>()

  init(_ keyPath: KeyPath<T, V>, on object: T) {
    value = object[keyPath: keyPath]
    object.publisher(for: keyPath)
      .assign(to: \.value, on: self)
      .store(in: &cancel)
  }
}

struct ContentView: View {
  @ObservedObject var defaultsObserver = KeyPathObserver(\.myCount, on: defaults)

  var body: some View {
    VStack {
      Text("myCount: \(defaults.myCount)")
      Text("myCountSquared: \(defaults.myCountSquared)")
      Button(action: {
        defaults.set(defaults.myCount + 1, forKey: "myCount")
      }) {
        Text("Increment")
      }
    }
  }
}
let viewController = UIHostingController(rootView: ContentView())
PlaygroundPage.current.liveView = viewController

请注意,我们在扩展中添加了一个附加属性myCountSquaredUserDefaults计算派生值,但请注意原始的KeyPath.

截屏

于 2019-11-25T17:21:19.940 回答
8

当 Swift 更新为启用嵌套属性包装器时,执行此操作的方法可能是创建一个@UserDefault属性包装器并将其与@Published.

同时,我认为处理这种情况的最好方法是ObservableObject手动实现而不是依赖@Published. 像这样的东西:

class ViewModel: ObservableObject {
    let objectWillChange = ObservableObjectPublisher()

    var name: String {
        get {
            UserDefaults.standard.string(forKey: "name") ?? ""
        }
        set {
            objectWillChange.send()
            UserDefaults.standard.set(newValue, forKey: "name")
        }
    }
}

属性包装器

正如我在评论中提到的,我认为没有办法将其包装在一个删除所有样板的属性包装器中,但这是我能想到的最好的:

@propertyWrapper
struct PublishedUserDefault<T> {
    private let key: String
    private let defaultValue: T

    var objectWillChange: ObservableObjectPublisher?

    init(wrappedValue value: T, key: String) {
        self.key = key
        self.defaultValue = value
    }

    var wrappedValue: T {
        get {
            UserDefaults.standard.object(forKey: key) as? T ?? defaultValue
        }
        set {
            objectWillChange?.send()
            UserDefaults.standard.set(newValue, forKey: key)
        }
    }
}

class ViewModel: ObservableObject {
    let objectWillChange = ObservableObjectPublisher()

    @PublishedUserDefault(key: "name")
    var name: String = "John"

    init() {
        _name.objectWillChange = objectWillChange
    }
}

您仍然需要以objectWillChange某种方式声明并将其连接到您的属性包装器(我正在这样做init),但至少属性定义本身非常简单。

于 2019-11-25T20:05:27.877 回答
3

更新:使用 EnclosureSelf 下标,可以做到!

奇迹般有效!

import Combine
import Foundation

class LocalSettings: ObservableObject {
  static var shared = LocalSettings()

  @Setting(key: "TabSelection")
  var tabSelection: Int = 0
}

@propertyWrapper
struct Setting<T> {
  private let key: String
  private let defaultValue: T

  init(wrappedValue value: T, key: String) {
    self.key = key
    self.defaultValue = value
  }

  var wrappedValue: T {
    get {
      UserDefaults.standard.object(forKey: key) as? T ?? defaultValue
    }
    set {
      UserDefaults.standard.set(newValue, forKey: key)
    }
  }

  public static subscript<EnclosingSelf: ObservableObject>(
    _enclosingInstance object: EnclosingSelf,
    wrapped wrappedKeyPath: ReferenceWritableKeyPath<EnclosingSelf, T>,
    storage storageKeyPath: ReferenceWritableKeyPath<EnclosingSelf, Setting<T>>
  ) -> T {
    get {
      return object[keyPath: storageKeyPath].wrappedValue
    }
    set {
      (object.objectWillChange as? ObservableObjectPublisher)?.send()
      UserDefaults.standard.set(newValue, forKey: object[keyPath: storageKeyPath].key)
    }
  }
}

于 2019-11-27T10:03:59.103 回答
1

现在我们有@AppStorage这个:

应用存储

一种属性包装器类型,它反映来自 UserDefaults 的值,并使该用户默认值中的值更改的视图无效。

https://developer.apple.com/documentation/swiftui/appstorage

于 2020-10-15T19:57:13.993 回答