[英]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
. 当我运行以下代码时,即使我以xcode进入调试器控制台并执行
po [resultObject valueForKey:@"type"]
,也无法将它放入if
语句中,它返回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. valueForKey:
的结果始终是一个对象-唯一等于0的对象为nil。 In the case of a numerical value, it will be an NSNumber. 如果是数值,则为NSNumber。 At any rate, I think you want to ask for
[[resultObject valueForKey:@"type"] intValue]
. 无论如何,我想您想问一下
[[resultObject valueForKey:@"type"] intValue]
。
You could try casting the NSManagedObject to an int (if thats what it actually is...) 您可以尝试将NSManagedObject强制转换为int(如果那实际上是...)
Also you don't need the extra parentheses around the [ ] 另外,您也不需要在[]周围加上多余的括号
NSManagedObject *resultObject = [qResult objectAtIndex:i];
if ((int)[resultObject valueForKey:@"type"] == 0) {
//do something
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.