Brittany Brittany - 5 months ago 20
Objective-C Question

Saving to NSUserDefaults crashes app?

I'm trying to save a session to NSUserDefaults with the code below, and for some reason doing so is crashing my app with the following error.


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@mattermedia.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


Any idea why?

Thanks!

[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; }
];

}

Answer

"Any idea why?". Yes:

[[NSUserDefaults standardUserDefaults] setObject:[[DIOSSession sharedSession] user]
                                          forKey:@"diosSession"];

I don't know what [[DIOSSession sharedSession] user] is, but only a NSArray, NSDictionary, NSString, NSData, NSNumber and NSDate may be stored.

So I guess that's why.

Comments