我试图保存 UICollectionView 的索引路径,但出现以下错误:
libc++abi.dylib: terminating with uncaught exception of type NSException
我的代码是:
保存索引路径:
[[NSUserDefaults standardUserDefaults] setObject:indexPath forKey:@"CollectionIndex"];
[[NSUserDefaults standardUserDefaults] synchronize];
加载索引路径:
NSIndexPath *storedIndexPath = [[NSUserDefaults standardUserDefaults] objectForKey:@"CollectionIndex"];
我已经注释掉了代码以找出导致错误的代码,这正是我用来保存创建 SIGABRT 错误的 indexPath 的代码。
阅读以下文档NSUserDefaults > setObjectForKey
:
“value 参数只能是属性列表对象:NSData、NSString、NSNumber、NSDate、NSArray 或 NSDictionary。对于 NSArray 和 NSDictionary 对象,其内容必须是属性列表对象。请参阅“什么是属性列表?”在属性列表编程指南中。”
要在 UserDefaults 中保存索引路径,您必须使用另一种表示形式,例如作为字典:
[[NSUserDefaults standardUserDefaults] setObject:@{@"row": @(indexPath.row),
@"section": @(indexPath.section)}
forKey:@"CollectionIndex"];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)