How to check for Nil

后端 未结 4 1186
日久生厌
日久生厌 2021-01-21 13:05

I am currently studying objective-c and the basic c programming language.

I have a questions about a particular line of code:

if (!balance)
4条回答
  •  长发绾君心
    2021-01-21 13:45

    In Objective-C, nil is roughly analogous to 0, NULL or false, but for object pointers. In an if statement, it will behave the same as one of the aforementioned scalar values. For example, the following two if statements should produce the same results:

    NSNumber *balance = nil;
    
    if (!balance) {
        // do something if balance is nil
    }
    
    if (balance == nil) {
        // do something if balance is nil
    }
    

提交回复
热议问题