Hello. I am using NSUserDefaults to save values, e.g.
let defaultsLoad = NSUserDefaults.standardUserDefaults()
// Strings
string1 = defaultsLoad.stringForKey("String1")
// Arrays
let array1 = defaultsLoad.objectForKey("Array1") as? NSData
if let array1 = array1
{
self.array = NSKeyedUnarchiver.unarchiveObjectWithData(array1) as! [String]
}
My question is: how do I save and retrieve tuples in swift? I have a tuple variable
(String,String,String)
and an array of these tuples:
[(String,String,String)]
These do not work:
let defaultsLoad = NSUserDefaults.standardUserDefaults()
defaultsLoad.setObject(tuple1, forKey:"Tuple1")
let tupleArrayData = NSKeyedArchiver.archivedDataWithRootObject(tupleArray)
defaultsLoad.setObject(tupleArrayData, forKey: "TupleArray")
let defaultsLoad = NSUserDefaults.standardUserDefaults()
tuple1 = defaultsLoad.objectForKey("Tuple1")
// This one does not show any error. The problem is with saving
let tupleArrayData = defaultsLoad.objectForKey("TupleArray") as? NSData
if let tupleArrayData = tupleArrayData
{
tupleArray = NSKeyedUnarchiver.unarchiveObjectWithData(tupleData) as! [(String,String,String)]
}
Thanks a lot!