2

我知道它@AppStorage只能在视图中使用,但我更喜欢管理模型并利用@AppStorage.

例如,如果我想将购买的产品存储在 中In App Purchasing,我可以使用此解决方法还是最好避免这样做?

typealias PurchasedProductIdentifiers = Set<String>

extension PurchasedProductIdentifiers: RawRepresentable {
    public init?(rawValue: String) {
        guard let data = rawValue.data(using: .utf8),
            let result = try? JSONDecoder().decode(PurchasedProductIdentifiers.self, from: data)
        else {
            return nil
        }
        self = result
    }
    public var rawValue: String {
        guard let data = try? JSONEncoder().encode(self),
            let result = String(data: data, encoding: .utf8)
        else {
            return "[]"
        }
        return result
    }
}

class MyModel: NSObject, ObservableObject {
    
    @Published var purchasedPub: PurchasedProductIdentifiers = [] {
        didSet {
            purchased = purchasedPub
        }
    }
    @AppStorage("purchased") private var purchased = PurchasedProductIdentifiers(["p1"])
    
    override init() {
        super.init()
        self.purchasedPub = self.purchased
    }
}

struct TestView: View {

    @StateObject var model: MyModel = MyModel() 
        
    var body: some View {
        
        VStack (alignment: .leading){
            HStack {
                Image(systemName: model.purchasedPub.contains("p1") ? "lock.open":"lock")
                Text("\("p1")")
                Button(action: {
                    model.purchasedPub.insert("p1")
                }, label: {
                    Text("buy")
                })
                Button(action: {
                    model.purchasedPub.remove("p1")
                }, label: {
                    Text("remove")
                })
            }
            HStack {
                Image(systemName: model.purchasedPub.contains("p2") ? "lock.open":"lock")
                Text("\("p2")")
                Button(action: {
                    model.purchasedPub.insert("p2")
                }, label: {
                    Text("buy")
                })
                Button(action: {
                    model.purchasedPub.remove("p2")
                }, label: {
                    Text("remove")
                })
            }
        }
    }
}



struct TestView_Previews: PreviewProvider {
    static var previews: some View {
        TestView()
    }
}
4

1 回答 1

0

在 ObservableObject: 上添加willSet {..}并调用 send() objectWillChange.send()

例如:

@AppStorage("purchased") 
private var purchased = PurchasedProductIdentifiers(["p1"]) {
   willSet {
      DispatchQueue.main.async {
         self.objectWillChange.send()
      }
   }
}

于 2021-02-23T21:57:51.543 回答