__block NSArray *arrayImgAssetURL = [[NSUserDefaults standardUserDefaults] objectForKey:@"imgAssetURL"];
// if img url is not available then find image url and save to user default and
if (!arrayImgAssetURL) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
arrayImgAssetURL = [[GALAssetsLibrary sharedReference] readImagesFromGallery];
// write asset image url to user default
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:arrayImgAssetURL
forKey:@"imgAssetURL"];
[userDefaults synchronize];
});
}
上面的方法在[userDefaults setObject:arrayImgAssetURL forKey:@“imgAssetURL”]中崩溃;
方法[[GALAssetsLibrary sharedReference] readImagesFromGallery];返回__block NSArray对象,并将其分配给具有值的arrayImgAssetURL。只有当我尝试插入userDefault时才会崩溃。 NSUserDefaults只允许插入(数组,数据,字符串,数字,日期,字典)。然后为什么会崩溃?这是什么解决方案?
崩溃日志
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '*** -[NSUserDefaults setObject:forKey:]:
attempt to insert non-property list object (
"assets-library://asset/asset.JPG?id=E3DCD905-082D-4D0C-886E-55CDFC669D41&ext=JPG",
"assets-library://asset/asset.JPG?id=F471F275-AD0A-489F-9330-CB8A51571087&ext=JPG",
"assets-library://asset/asset.JPG?id=7F313D5B-CA0E-493C-9A1E-AC15BABFC741&ext=JPG",
"assets-library://asset/asset.JPG?id=2270D5A3-0741-4278-9021-79FA89AAB6CB&ext=JPG",
"assets-library://asset/asset.JPG?id=239747B8-0586-490E-BCF1-6BB5675D47EE&ext=JPG",
"assets-library://asset/asset.JPG?id=882487BA-1A54-4B97-8B56-DB5409AB6262&ext=JPG",
"assets-library://asset/asset.JPG?id=88CDC266-CE00-44AB-A24B-9630B27E2AED&ext=JPG",
"assets-library://asset/asset.JPG?id=3B6B3C09-9839-48C2-91EA-B26691445892&ext=JPG"
) for key imgAssetURL'
*** First throw call stack:
(
0 CoreFoundation 0x017f21e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x015718e5 objc_exception_throw + 44
2 CoreFoundation 0x017f1fbb +[NSException raise:format:] + 139
3 Foundation 0x011a4a40 -[NSUserDefaults(NSUserDefaults) setObject:forKey:] + 150
4 FaceDetection 0x0000296d __59-[MCAppDelegate application:didFinishLaunchingWithOptions:]_block_invoke + 237
5 libdispatch.dylib 0x01e377b8 _dispatch_call_block_and_release + 15
6 libdispatch.dylib 0x01e4c4d0 _dispatch_client_callout + 14
7 libdispatch.dylib 0x01e3aeb7 _dispatch_root_queue_drain + 291
8 libdispatch.dylib 0x01e3b127 _dispatch_worker_thread2 + 39
9 libsystem_pthread.dylib 0x021772e3 _pthread_wqthread + 801
10 libsystem_pthread.dylib 0x02174eea start_wqthread + 30
)
libc++abi.dylib: terminating with uncaught exception of type NSException