Save tuple in NSUserDefaults

I use a tuple to store something like this.

var accessLavels: (hasInventoryAccess: Bool, hasPayrolAccess: Bool) accessLavels = (hasInventoryAccess: true, hasPayrolAccess: false) 

Now I want to save it to NSUserDefaults .

 NSUserDefaults.standardUserDefaults().setValue(accessLavels, forKey: "AccessLevelKey") NSUserDefaults.standardUserDefaults().synchronize() 

But I get the following error.

Type '(hasInventoryAccess: Bool, hasPayrolAccess: Bool)' does not conform to AnyObject protocol

How can I solve this problem? If this is not possible, then any other suggestions for maintaining the tuple are welcome.

Thanks.

+6
source share
5 answers

I came across a similar scenario trying to encode a tuple using NSCoder . The way I solve this is to manually convert the tuple to Dictionary . This is not a great solution, as the keys need to be changed in several places if the tuple ever changes.

I had a nested enum in my tuple and gave it the base type (String) from which I converted the raw value. It was a little extra work, but, fortunately, yours are only primitives.

 # SerializeableTuple.swift typealias AccessTuple = (hasInventoryAccess: Bool, hasPayrolAccess: Bool) typealias AccessDictionary = [String: Bool] let InventoryKey = "hasInventoryAccess" let PayrollKey = "hasPayrollAccess" func serializeTuple(tuple: AccessTuple) -> AccessDictionary { return [ InventoryKey : tuple.hasInventoryAccess, PayrollKey : tuple.hasPayrolAccess ] } func deserializeDictionary(dictionary: AccessDictionary) -> AccessTuple { return AccessTuple( dictionary[InventoryKey] as Bool!, dictionary[PayrollKey] as Bool! ) } 

 # Encoding / Decoding var accessLavels: AccessTuple = (hasInventoryAccess: true, hasPayrolAccess: false) // Writing to defaults let accessLevelDictionary = serializeTuple(accessLavels) NSUserDefaults.standardUserDefaults().setObject(accessLevelDictionary, forKey: "AccessLevelKey") // Reading from defaults let accessDic = NSUserDefaults.standardUserDefaults().dictionaryForKey("AccessLevelKey") as AccessDictionary let accessLev = deserializeDictionary(accessDic) 
+7
source

I had a tuple with 3 values.

The following code was used to save the tuple. Basically, I created a string from a tuple (with components separated by a comma).

 let defaultsLoad = NSUserDefaults.standardUserDefaults() if appSingleton.runwayDestShared != nil { // Creating a String from the tuple let runwayDestString = appSingleton.runwayDestShared!.0 + "," + appSingleton.runwayDestShared!.1 + "," + appSingleton.runwayDestShared!.2 defaultsLoad.setObject(runwayDestString, forKey: "RunwayDest") } 

To get a tuple, I extracted a string, split it into an array, and used the array to recreate the tuple.

 let runwayDestString = defaultsLoad.stringForKey("RunwayDest") if let runwayDestString = runwayDestString { let runwayDestArray = runwayDestString.componentsSeparatedByString(",") appSingleton.runwayDestShared = (runwayDestArray[0],runwayDestArray[1],runwayDestArray[2]) } 
+1
source

In the documentation, I do not see any method -setValue . Based on documents, NSUserDefaults can only store NSString, NSNumber, NSData, NSDictionary, NSArray, NSData and NSDate. Link here: NSUserDefaults Class Reference.

So, you cannot save the tuple here. And -setValue is the NSKeyValueCoding protocol.

0
source

You can store Bool, Float, Int, Object, Double or URL, but not Tuple. So, you have two options, save two hasPayrolAccess and hasPayrolAccess Bool values โ€‹โ€‹only:

 NSUserDefaults.standardUserDefaults().setBool(true, forKey: "hasInventoryAccess") NSUserDefaults.standardUserDefaults().setBool(false, forKey: "hasPayrolAccess") let hasInventoryAccess = NSUserDefaults.standardUserDefaults().boolForKey("hasInventoryAccess") println(hasInventoryAccess) let hasPayrolAccess = NSUserDefaults.standardUserDefaults().boolForKey("hasPayrolAccess") println(hasPayrolAccess) 

Or save it using a Bool array:

 var accessLavels = [true,false] println(accessLavels) NSUserDefaults.standardUserDefaults().setValue(accessLavels, forKey: "accessLavels") if let loadAccessLavels = NSUserDefaults.standardUserDefaults().arrayForKey("accessLavels") as? [Bool] { if let hasInventoryAccess = loadAccessLavels.first { println(hasInventoryAccess) } if let hasPayrolAccess = loadAccessLavels.last { println(hasPayrolAccess) } } 
0
source

I asked one year, but still:

  let accesLvl : [String:AnyObject] = ["hasInventoryAcces":true, "hasPayrolAccess":false] NSUserDefaults.standardUserDefaults().setObject(accesLvl, forKey: "accesLevel") 

If you save only bools, let accesLvl : [String:Bool] is the best option.

In case I get nothing (I'm pretty new to Swift and generally program), it would be useful to use a "tuple" over the Dictionary or even a struct

0
source

Source: https://habr.com/ru/post/980858/


All Articles