I am trying to convert a NSNumber to long but I get this error:
[__NSSingleObjectArrayI intValue]: unrecognized selector sent to instance
Here is my code:
NSNumber *dbversion = [settings valueForKey:@"Version"];
long dbver = [dbversion longValue];
What am I doing wrong here?
* is a NSArray and "Version" is the key for a long value. 是一个NSArray,“版本”是长值的关键。
You are caught in the Key-Value Coding trap.
In some cases the result of valueForKey
is an array which the error message clearly states.
Don't
Never use valueForKey
(unless you know what KVC does and you need KVC), use key subscription.
And as settings
is an array you might get the first item
NSNumber *dbversion = settings[0][@"Version"];
and int
is not long
long dbver = [dbversion longValue];
However on a 64-bit machine I recommend to use NSInteger
NSInteger dbver = dbversion.integerValue;
//I think you are storing string from dictionary "settings" to NSNumber so it's showing error like you mention in question. Please Try with solution. May this help you.
NSNumber *dbversion = [NSNumber numberWithLong:[[settings valueForKey:@"Version"] longLongValue]];
int dbver = [dbversion longValue];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.