views:

1730

answers:

2

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
}
+1  A: 

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
}
James Raybould
This will just check if the object is nil and is essentially equivalent to the original example. If will not check if the object's value is 0.
Chuck
+8  A: 

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].

Chuck
Thank you! This worked perfectly! I knew it was something simple like this.
ABB