So if you need to represent a "nil", you can use [NSNull null]. There is a difference, but only to yourself and other humans that read the code, the compiler doesn't care. They differ in their types. I see lots of example code on the web where people provide nil instead of NULL in this case. This is because it's a pointer to a pointer and thus not directly an Objective-C object type. As said above, nil should be used for Objective-C object types. Here [NSNULL null] is an object which means no object, at the same time you cannot add nil to indicate absence of object.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 10 years, 8 months ago. Active 6 years, 1 month ago. Viewed 34k times. An Objective-C object is an instance of an Objective-C class the one which is declared via interface. The instance is usuallay created with [[MyClass alloc] init] or [MyClass new].
In ObjC difference between object and class is the same as in any other language. If you don't know that - you should learn the OOP basics. Add a comment. Active Oldest Votes. You should also note that it is possible to send messages to nil without getting segmentation faults. This can be reflected to swift?
Like, is in swift a correspondence for that? This is correct. I think in practice it doesn't really matter which you use. But follow the convention. For example, IBAction is defined as void but it has a different meaning when using interface builder when displaying methods to attach to actions on buttons and such.
They're technically the same thing and differ only in style: Objective-C style says nil is what to use for the id type and pointers to objects. I typically use the variant that matches the language where the type is declared. Andrew Andrew 1, 12 12 silver badges 11 11 bronze badges. They almost are the same thing except, nil is used in an Objective-C style. Isn't this pretty much a repeat of this existing answer? Sign up or log in Sign up using Google.
Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Stack Gives Back Perhaps the most notable behavior of nil , however, is that it can have messages sent to it without a problem. In other languages, like Java, this kind of behavior would crash your program. This fact alone significantly simplifies things for Objective-C developers, as it obviates the need to check for nil before doing anything:.
With vigilance, this quirk of Objective-C can be a convenient feature rather than a lurking source of bugs in your application.
0コメント