将存储在NSUserDefaults中

我想保存一个Swift样式string数组到NSUserDefaults,但在代码中的“if”语句实际上说returnValue始终为零。

在后面的代码中,我想用“food + = [”意大利面条“]添加新条目。

var food : [String] { get { var returnValue : [String]? = NSUserDefaults.standardUserDefaults().objectForKey("food") as? [String] if returnValue == nil //Check for first run of app { returnValue = ["muesli", "banana"]; //Default value } return returnValue! } set (newValue) { NSUserDefaults.standardUserDefaults().setObject(newValue, forKey: "food") NSUserDefaults.standardUserDefaults().synchronize() } } 

下面的代码应该可以帮助你解决你的问题:

 import UIKit class ViewController: UIViewController { var food: [String] { get { if let returnValue = NSUserDefaults.standardUserDefaults().objectForKey("food") as? [String] { return returnValue } else { return ["muesli", "banana"] //Default value } } set { NSUserDefaults.standardUserDefaults().setObject(newValue, forKey: "food") NSUserDefaults.standardUserDefaults().synchronize() } } override func viewDidLoad() { super.viewDidLoad() print(food) // prints: ["muesli", "banana"] (at first launch) food = ["cake"] print(food) // prints: ["cake"] food += ["spaghetti"] print(food) // prints: ["cake", "spaghetti"] food = [] print(food) // prints: [] NSUserDefaults.standardUserDefaults().setObject(nil, forKey: "food") print(food) // prints: ["muesli", "banana"] } } 

但是,用前面的代码,如果你设置food = [] ,你将会遇到一个问题,因为food不会返回["muesli", "banana"] 。 为了避免这种情况,你可能更喜欢下面的代码:

 import UIKit class ViewController: UIViewController { var food: [String] { get { if let returnValue = NSUserDefaults.standardUserDefaults().objectForKey("food") as? [String] { return returnValue == [] ? ["muesli", "banana"] : returnValue } else { return ["muesli", "banana"] //Default value } } set { NSUserDefaults.standardUserDefaults().setObject(newValue, forKey: "food") NSUserDefaults.standardUserDefaults().synchronize() } } override func viewDidLoad() { super.viewDidLoad() print(food) // prints: ["muesli", "banana"] (at first launch) food = ["cake"] print(food) // prints: ["cake"] food += ["spaghetti"] print(food) // prints: ["cake", "spaghetti"] food = [] print(food) // prints: ["muesli", "banana"] NSUserDefaults.standardUserDefaults().setObject(nil, forKey: "food") print(food) // prints: ["muesli", "banana"] } } 

正如文件中所述:

对于NSArray和NSDictionary对象,其内容必须是属性列表对象。

这意味着你需要在保存时将你的String对象转换为NSString ,像这样的东西应该工作:

 var food : [String] { get { var returnValue : [String]? = NSUserDefaults.standardUserDefaults().objectForKey("food") as? [String] if returnValue == nil //Check for first run of app { returnValue = ["muesli", "banana"]; //Default value } return returnValue! } set (newValue) { // Each item in newValue is now a NSString let val = newValue as [NSString] NSUserDefaults.standardUserDefaults().setObject(val, forKey: "food") NSUserDefaults.standardUserDefaults().synchronize() } } 

在Swift 3.0中

商店

 UserDefaults.standard.set(newValue, forKey: "yourkey") UserDefaults.standard.synchronize() 

取回

 var returnValue: [NSString]? = UserDefaults.standard.object(forKey: "yourkey") as? [NSString] 

去掉

 UserDefaults.standard.removeObject(forKey: "yourkey") 

参考: NSUserdefault objectTypes

使用函数设置并获取NSUserDefault

使用下面的函数全局使用,在任何类中声明任何在整个应用程序中的使用

声明

 func setUserDefault(ObjectToSave : AnyObject? , KeyToSave : String) { let defaults = NSUserDefaults.standardUserDefaults() if (ObjectToSave != nil) { defaults.setObject(ObjectToSave, forKey: KeyToSave) } NSUserDefaults.standardUserDefaults().synchronize() } func getUserDefault(KeyToReturnValye : String) -> AnyObject? { let defaults = NSUserDefaults.standardUserDefaults() if let name = defaults.valueForKey(KeyToReturnValye) { return name } return nil } 

如何打电话

 Classname_where_this_function_declare.sharedInstance.setUserDefault("value", KeyToSave: "key")** 

我们可以使用下面的代码来创build一个对象: }

// class func loadCustomObject(_ key:String) – > AnyObject? {let userDefaults = UserDefaults.standard if let userDefaultKey = userDefaults.object(forKey:key){let encodedObject = userDefaultKey as! NSData返回NSKeyedUnarchiver.unarchiveObject(与:encodedObject作为数据)! 作为AnyObject? } else {return nil}}

//

class func removeCustomObject(_ key:String){//让encodedObject = NSKeyedArchiver.archivedDataWithRootObject(object!)let userDefaults = UserDefaults.standard userDefaults.removeObject(forKey:key)userDefaults.synchronize()}

调用方法

让aParams:[String:String] = [“DeviceType”:“iOS”,] CommonMethods.saveCustomObject(aParams as AnyObject ?,键:Constants.kLoginData)