保存到 NSUserDefaults 会导致应用程序崩溃吗?
Saving to NSUserDefaults crashes app?
我正在尝试使用下面的代码将会话保存到 NSUserDefaults,但出于某种原因这样做会导致我的应用程序崩溃并出现以下错误。
2015-10-31 12:38:22.327 App[13930:4844726] *** Terminating app due
to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt
to insert non-property list object {type = immutable dict, count = 17, entries => 0 :
status = 1 1 : theme = {contents
= ""} 2 : roles = {type = immutable dict, count = 2, entries => 0 : 2 = {contents = "authenticated user"} 1 : 3 = {contents = "administrator"} }
3 : uid = 1 5 : access = {contents = "1446319867"} 6 : login = {value = +1446320300, type =
kCFNumberSInt64Type} 7 : picture =
9 : {contents =
"signature_format"} = 10 : init =
{contents =
"brittany@email.ca"} 13 : signature = {contents = ""} 14 : data = {value = false} 15 : language = {contents = ""} 16 : created = {contents = "1428187324"} 17 : {contents = "rdf_mapping"} = {type = immutable dict, count = 3, entries => 0 :
rdftype = {type = immutable, count
= 1, values = ( 0 : {contents = "sioc:UserAccount"} )} 1 : name = {type = immutable dict, count = 1, entries => 2 :
predicates = {type = immutable,
count = 1, values = ( 0 : {contents = "foaf:name"} )} }
2 : homepage = {type =
immutable dict, count = 2, entries => 0 : type = rel 2 : predicates
= {type = immutable, count = 1, values = ( 0 : {contents =
"foaf:page"} )} }
}
18 : timezone = {contents =
"America/Los_Angeles"} 21 : name = admin 22 : mail = {contents = "email@email.ca"} }
for key diosSession'
*** First throw call stack: (0x183db8f5c 0x1989aff80 0x183db8ea4 0x183df5970 0x183d427c0 0x183d416d8 0x183df5c74 0x183df520c
0x183df8a1c 0x184c21138 0x1000ef5c8 0x1000f14fc 0x100120850
0x1000e8248 0x10026dd70 0x10026dd30 0x100273780 0x183d70258
0x183d6e0c0 0x183c9cdc0 0x18edf0088 0x189376f44 0x100159674
0x1991da8b8) libc++abi.dylib: terminating with uncaught exception of
type NSException
知道为什么吗?
谢谢!
[DIOSUser userLoginWithUsername:_userField.text
andPassword:_passField.text
success:^(AFHTTPRequestOperation *op, id response) {
// Saving to keychain/NSUserDefaults
[[NSUserDefaults standardUserDefaults] setObject:[[DIOSSession sharedSession] user]
forKey:@"diosSession"];
[[NSUserDefaults standardUserDefaults] synchronize];
[[DIOSSession sharedSession] getCSRFTokenWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *csrfToken = [NSString stringWithUTF8String:[responseObject bytes]];
[[NSUserDefaults standardUserDefaults] setObject:csrfToken forKey:@"diosToken"];
[[NSUserDefaults standardUserDefaults] synchronize];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// failure handler
}];
wrongLogin.hidden = YES;
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
MyAccountViewController *yourViewController = (MyAccountViewController *)[storyboard instantiateViewControllerWithIdentifier:@"MyAccount"];
[self.navigationController pushViewController:yourViewController animated:YES];
[self.activityIndicatorViewOne stopAnimating];
self.activityIndicatorViewOne.hidden = YES;
NSLog(@"Success!");}
failure:^(AFHTTPRequestOperation *op, NSError *err) { NSLog(@"Fail!"); wrongLogin.hidden = NO; }
];
}
"Any idea why?"。是:
[[NSUserDefaults standardUserDefaults] setObject:[[DIOSSession sharedSession] user]
forKey:@"diosSession"];
我不知道[[DIOSSession sharedSession] user]
是什么,只知道NSArray
、NSDictionary
、NSString
、NSData
、NSNumber
NSDate
可能会被存储。
所以我想这就是原因。
请试试这个
用于设置对象:
NSData *yourData = [NSKeyedArchiver archivedDataWithRootObject:[[DIOSSession sharedSession] user]];
[[NSUserDefaults standardUserDefaults] setObject:yourData forKey:@"yourKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
获取对象:
NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"yourKey"];
DIOSSession *object = [NSKeyedUnarchiver unarchiveObjectWithData:data];
我正在尝试使用下面的代码将会话保存到 NSUserDefaults,但出于某种原因这样做会导致我的应用程序崩溃并出现以下错误。
2015-10-31 12:38:22.327 App[13930:4844726] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object {type = immutable dict, count = 17, entries => 0 : status = 1 1 : theme = {contents = ""} 2 : roles = {type = immutable dict, count = 2, entries => 0 : 2 = {contents = "authenticated user"} 1 : 3 = {contents = "administrator"} }
3 : uid = 1 5 : access = {contents = "1446319867"} 6 : login = {value = +1446320300, type = kCFNumberSInt64Type} 7 : picture = 9 : {contents = "signature_format"} = 10 : init = {contents = "brittany@email.ca"} 13 : signature = {contents = ""} 14 : data = {value = false} 15 : language = {contents = ""} 16 : created = {contents = "1428187324"} 17 : {contents = "rdf_mapping"} = {type = immutable dict, count = 3, entries => 0 : rdftype = {type = immutable, count = 1, values = ( 0 : {contents = "sioc:UserAccount"} )} 1 : name = {type = immutable dict, count = 1, entries => 2 : predicates = {type = immutable, count = 1, values = ( 0 : {contents = "foaf:name"} )} }
2 : homepage = {type = immutable dict, count = 2, entries => 0 : type = rel 2 : predicates = {type = immutable, count = 1, values = ( 0 : {contents = "foaf:page"} )} }
}
18 : timezone = {contents = "America/Los_Angeles"} 21 : name = admin 22 : mail = {contents = "email@email.ca"} } for key diosSession' *** First throw call stack: (0x183db8f5c 0x1989aff80 0x183db8ea4 0x183df5970 0x183d427c0 0x183d416d8 0x183df5c74 0x183df520c 0x183df8a1c 0x184c21138 0x1000ef5c8 0x1000f14fc 0x100120850 0x1000e8248 0x10026dd70 0x10026dd30 0x100273780 0x183d70258 0x183d6e0c0 0x183c9cdc0 0x18edf0088 0x189376f44 0x100159674 0x1991da8b8) libc++abi.dylib: terminating with uncaught exception of type NSException
知道为什么吗?
谢谢!
[DIOSUser userLoginWithUsername:_userField.text
andPassword:_passField.text
success:^(AFHTTPRequestOperation *op, id response) {
// Saving to keychain/NSUserDefaults
[[NSUserDefaults standardUserDefaults] setObject:[[DIOSSession sharedSession] user]
forKey:@"diosSession"];
[[NSUserDefaults standardUserDefaults] synchronize];
[[DIOSSession sharedSession] getCSRFTokenWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *csrfToken = [NSString stringWithUTF8String:[responseObject bytes]];
[[NSUserDefaults standardUserDefaults] setObject:csrfToken forKey:@"diosToken"];
[[NSUserDefaults standardUserDefaults] synchronize];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// failure handler
}];
wrongLogin.hidden = YES;
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
MyAccountViewController *yourViewController = (MyAccountViewController *)[storyboard instantiateViewControllerWithIdentifier:@"MyAccount"];
[self.navigationController pushViewController:yourViewController animated:YES];
[self.activityIndicatorViewOne stopAnimating];
self.activityIndicatorViewOne.hidden = YES;
NSLog(@"Success!");}
failure:^(AFHTTPRequestOperation *op, NSError *err) { NSLog(@"Fail!"); wrongLogin.hidden = NO; }
];
}
"Any idea why?"。是:
[[NSUserDefaults standardUserDefaults] setObject:[[DIOSSession sharedSession] user]
forKey:@"diosSession"];
我不知道[[DIOSSession sharedSession] user]
是什么,只知道NSArray
、NSDictionary
、NSString
、NSData
、NSNumber
NSDate
可能会被存储。
所以我想这就是原因。
请试试这个
用于设置对象:
NSData *yourData = [NSKeyedArchiver archivedDataWithRootObject:[[DIOSSession sharedSession] user]];
[[NSUserDefaults standardUserDefaults] setObject:yourData forKey:@"yourKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
获取对象:
NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"yourKey"];
DIOSSession *object = [NSKeyedUnarchiver unarchiveObjectWithData:data];