How to check for Nil

后端 未结 4 1188
日久生厌
日久生厌 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:58

    If balance is nil then it will be a pointer to 0x0. That address is never used for a valid object.

    In C anything within an if that evaluates to zero is considered a negative response, anything that evaluates to non-zero is a positive response.

    Pointers evaluate to their address — exactly as if you cast them to an integral type. The ! means "NOT".

    So the test is if(address of balance is not zero).

提交回复
热议问题