Default value of an Objective-C struct and how to test

后端 未结 7 1227
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-31 15:33

I\'m trying to test if a property has been set yet. I know that with objects that I\'ve got:

CGRect ppGoalFrame;
LocalPlaySetup *localPlaySetup;
<
7条回答
  •  南旧
    南旧 (楼主)
    2021-01-31 16:12

    There is no universal "nothing" value. There's nil for objects and NULL for pointers, but there's no equivalent for plain value types — they always have some value, even if it's just nonsense. You can take an arbitrary value and say "I'm going to call this the null value for this type" (say, #define IntNull 4444444 and hope some calculation never accidentally returns that value), but there's no real null value.

    In the case of CGRect in particular, Apple used the "pick an arbitrary value" tactic and defined CGRectNull, but CGRect variables aren't set to that by default — you have to specifically set it yourself. By default, CGRect instance variables are set to CGRectZero, but remember that this is just a normal CGRect struct at location (0,0) with 0 width and 0 height. And local CGRects in a function have a totally unpredictable value.

提交回复
热议问题