简体   繁体   中英

objective-c if statement

So this is probably really simple but for some reason I can't figure it out. When I run the below code I can't get it to go into the if statement even though when I go into the debugger console in xcode and I execute po [resultObject valueForKey:@"type"] it returns 0 . What am I doing wrong? Thanks for your help!

NSManagedObject *resultObject = [qResult objectAtIndex:i];

if (([resultObject valueForKey:@"type"])== 0) {
    //do something
}

The result of valueForKey: is always an object — and the only object equal to 0 is nil. In the case of a numerical value, it will be an NSNumber. At any rate, I think you want to ask for [[resultObject valueForKey:@"type"] intValue] .

You could try casting the NSManagedObject to an int (if thats what it actually is...)

Also you don't need the extra parentheses around the [ ]

NSManagedObject *resultObject = [qResult objectAtIndex:i];

if ((int)[resultObject valueForKey:@"type"] == 0) {
    //do something
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM