I am trying to save NSIndexPath
at NSUserDefaults
, but got the following exception:-
Attempt to set a non-property-list object {length = 2, path = 0 - 12} as an NSUserDefaults/CFPreferences value for key LastSelectedIndeX
My code:-
let selectedIndexPath = tableView.indexPathForSelectedRow
NSUserDefaults.standardUserDefaults().setObject(selectedIndexPath, forKey: "lastSelectedIndex")
NSUserDefaults.standardUserDefaults().synchronize()
So how can I save NSIndexPath
so that I can use it later on at another view controller.
You can't save directly indexPath in NSUserDefault. You can Store NSArray , NSDictionary , NSNumber , NSString and NSDictionary in NSUserDefault.
Store IndexPath using NSKeyedArchiver
:
let data = NSKeyedArchiver.archivedDataWithRootObject(selectedIndexPath)
NSUserDefaults.standardUserDefaults().setObject(data, forKey: "indexKey")
Get IndexPath using NSKeyedArchiver
:
let data1 = NSUserDefaults.standardUserDefaults().objectForKey("indexKey") as? NSData
let indexP1 = NSKeyedUnarchiver.unarchiveObjectWithData(data1!) as! NSIndexPath