Error when trying to save indexPath to NSUserDefaults

十年热恋 提交于 2019-12-10 18:21:54

问题


I am trying to save the indexPath for UICollectionView, but I get the following error:

libc++abi.dylib: terminating with uncaught exception of type NSException

My code is: Save index path:

[[NSUserDefaults standardUserDefaults] setObject:indexPath forKey:@"CollectionIndex"];
[[NSUserDefaults standardUserDefaults] synchronize];

Load indexPath:

NSIndexPath *storedIndexPath = [[NSUserDefaults standardUserDefaults] objectForKey:@"CollectionIndex"];

I have commented out code to find out which code that makes the error, and it is the code I am using to save the indexPath that creates the SIGABRT error.


回答1:


Read the documentation of NSUserDefaults > setObjectForKey:

"The value parameter can be only property list objects: NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. For NSArray and NSDictionary objects, their contents must be property list objects. See “What is a Property List?” in Property List Programming Guide."

To save an indexPath in the UserDefaults, you would have to make another representation, e.g. as a dictionary:

[[NSUserDefaults standardUserDefaults] setObject:@{@"row": @(indexPath.row),
                                                   @"section": @(indexPath.section)}
                                          forKey:@"CollectionIndex"];


来源:https://stackoverflow.com/questions/21509911/error-when-trying-to-save-indexpath-to-nsuserdefaults

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!