iOS Equivalent For Android Shared Preferences

后端 未结 4 1586
挽巷
挽巷 2020-12-22 18:08

I am porting an Android app to iOS, one thing I used was the Shared Preferences in Android to save each time a level was complete.

That way when the user gets back i

4条回答
  •  礼貌的吻别
    2020-12-22 18:45

    class Configuration {
    
        static func value(defaultValue: T, forKey key: String) -> T{
    
            let preferences = UserDefaults.standard
            return preferences.object(forKey: key) == nil ? defaultValue : preferences.object(forKey: key) as! T
        }
    
        static func value(value: Any, forKey key: String){
    
            UserDefaults.standard.set(value, forKey: key)
        }
    
    }
    

    Example

    //set
    Configuration.value(value: "my_value", forKey: "key_1")
    
    //get
    let myValue = Configuration.value(defaultValue: "default_value", forKey: "key_1")
    

提交回复
热议问题