开发者

Objective-C AND operand is shortcircuited?

In Objective C does the Conditional-AND operator(&&) show short circuit b开发者_运维知识库ehavior as in Java? (Meaning right hand part will only be evaluated if left hand part is true) Strangely I couldn't find info on it.


Objective-c is a strict superset of c and basically follows the same standard. So as AND operator is short-circuited in c, it is the same in objective-c


Yes Objective C also uses short circuit evaluation, and will only evaluate what is necessary, just like Java and C.


anyway... you can test it by yourself, using a fake function that will raise an exception if invoked... like:

if (foo && [self raiseError]) {

}

if foo is false the error won't be raised


Yes. You can test it like I did:

NSArray *arr=nil;
    if ((arr!=nil)&&([[arr objectAtIndex:0] isEqualToString:@"testing"])) {[arr addObject:@"tested"];}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜