SwiftUI: What is @AppStorage property wrapper

前端 未结 5 2004
攒了一身酷
攒了一身酷 2020-12-19 03:34

I used to save important App data like login credentials into UserDefaults using the following statement:

UserDefaults.standard.set("sample@email.com&quo         


        
5条回答
  •  一生所求
    2020-12-19 04:16

    We can test this via simple approach:

    struct Content: View {
        
        private enum Keys {
        
            static let numberOne = "myKey"
        }
        
        @AppStorage(Keys.numberOne) var keyValue2: String = "no value"
    
        var body: some View {
            VStack {
                Button {
                    keyValue2 = "Hello"
                    print(
                        UserDefaults.standard.value(forKey: Keys.numberOne) as! String
                    )
                } label: {
                    Text("Update")
                }
                
                Text(keyValue2)
            }
            .padding()
            .frame(width: 100)
        }
    }
    

提交回复
热议问题