开发者

Objective-C AND operand is shortcircuited?

开发者 https://www.devze.com 2023-03-26 17:25 出处:网络
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) Strange

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

精彩评论

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