0

I'm teaching myself Objective-C 2.0.

I see from various code samples, the following two approaches to test if an object has been initialised. If that's not exactly what these tests do, please correct me.

Can you please explain the difference between the following:

if (!myObject)

and

if (myObject == nil)
4

1 回答 1

4

在 alloc 方法中,所有对象都设置为 nil(或实例变量设置为零)。您的两种情况都检查对象是否等于 nil (未初始化)并且两者都可以工作。它们彼此等价。你喜欢哪一个是一个口味问题。我个人倾向于使用

if (!myObject)

因为这是我个人的喜好。希望能帮助到你!

于 2013-07-17T10:00:49.003 回答