Search code examples
ioserror-handlingios-simulatornsnumber

Crashes on Simulator but not on devices: -[NSTaggedPointerString stringValue]: unrecognized selector sent to instance


I implemented some public function which can make me program more concisely. But I have a problem on my simulator but it's good on devices. I am 100% sure the uid I stored in UserDefaults is a NSNumber. But why it's only crashes on simulators?

Code:

+(NSString *) keyForUserProfileName{
    NSNumber *uid = [[NSUserDefaults standardUserDefaults] objectForKey:@"uid"];
    NSLog(@"my uid is %@", uid);
    NSString *key = [@"UserProfileNameFor" stringByAppendingString: [uid stringValue]];
    NSLog(@"my key is %@", key);

     return key;
}

Error:

my uid is 11803624
-[NSTaggedPointerString stringValue]: unrecognized selector sent to instance 0xa0079ea9d7eca1c8
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSTaggedPointerString stringValue]: unrecognized selector sent to instance 0xa0079ea9d7eca1c8'

uid assign code:

NSNumber *uid = [loginResult objectForKey:@"uid"];
[prefs setObject:uid forKey:@"uid"];

EDIT-----------------------------------------------------------------------

After I inserted these two lines after NSNumber *uid...line, the program goes through both simulators and devices. But I have totally no idea why it was wrong.

int uidint = [uid intValue];
NSString *uidString = [@(uidint) stringValue];

Solution

  • Change your uid assign code into

    NSNumber *uid = [NSNumber numberWithInteger:[[loginResult objectForKey:@"uid"] integerValue]];