nil, Nil, NULL, NSNull
nshipster.com
nil, Nil, NULL, NSNull
1–10 of 66 posts
Re: nil, Nil, NULL, NSNull
#2"C represents nothing as 0 for primitive value"
That's not true. There is no "nothing" for integer types, and floats either also lack "nothing", or have NaN as their "nothing", depending on your perspective.
One of the (many) annoying features of C-style NULL is that it means that some types are effectively option types while other types aren't, and there's no decent way to make them so. An NSString* is inherently "pointer to NSString or nothing", but an int is always an int, and I have to either carve out a special value to mean "nothing" (e.g. -1 used as the error return from a lot of UNIX syscalls) or use a separate flag.
Re: nil, Nil, NULL, NSNull
#3 // For example, this expression...
if (name != nil && [name isEqualToString:@"Steve"]) { ... }
// ...can be simplified to:
if ([name isEqualToString:@"steve"]) { ... }
Wow, that is some bad, bad code. Consider this snippet instead: if ([name isDifferentFromString:@"bob"]) { ... }Re: nil, Nil, NULL, NSNull
#4 NSString *val;
if(something){
val = @"hello";
}else if(somthingelse){
val = @"goodbye";
}
if(val){
[self display:val];
}
this can send you on a wild goose chase for not declaring val = nil initially.Re: nil, Nil, NULL, NSNull
#5Also for what its worth, variable declarations are not nil by default, this bug tied me up for quite some time. NSString *val; if(something){ val = @"hello"; }else if(somthingelse){ val = @"goodbye"; } if(val){ [self display:val]; } this can send you on a wild goose chase for not declaring val = nil initially.
Re: nil, Nil, NULL, NSNull
#6> In other languages, like C++, this would crash your program, but in Objective-C, invoking a method on nil returns a zero value. This greatly simplifies expressions, as it obviates the need to check for nil before doing anything: // For example, this expression... if (name != nil && [name isEqualToString:@"Steve"]) { ... } // ...can be simplified to: if ([name isEqualToString:@"steve"]) { ... } Wow, that is some bad…
Re: nil, Nil, NULL, NSNull
#7Also for what its worth, variable declarations are not nil by default, this bug tied me up for quite some time. NSString *val; if(something){ val = @"hello"; }else if(somthingelse){ val = @"goodbye"; } if(val){ [self display:val]; } this can send you on a wild goose chase for not declaring val = nil initially.
"Using ARC, strong, weak, and autoreleasing stack variables are now implicitly initialized with nil."
This doesn't affect plain C variables, but it would solve the issue you had above.
Re: nil, Nil, NULL, NSNull
#8Good article overall. However, I must object to this bit: "C represents nothing as 0 for primitive value" That's not true. There is no "nothing" for integer types, and floats either also lack "nothing", or have NaN as their "nothing", depending on your perspective. One of the (many) annoying features of C-style NULL is that it means that some types are effectively option types while other types aren't, and there's no…
But yeah, point well-taken. Perhaps I'll take the opportunity to wax on the other special values used in Foundation & CoreFoundation sometime.
Re: nil, Nil, NULL, NSNull
#9> In other languages, like C++, this would crash your program, but in Objective-C, invoking a method on nil returns a zero value. This greatly simplifies expressions, as it obviates the need to check for nil before doing anything: // For example, this expression... if (name != nil && [name isEqualToString:@"Steve"]) { ... } // ...can be simplified to: if ([name isEqualToString:@"steve"]) { ... } Wow, that is some bad…
Re: nil, Nil, NULL, NSNull
#10> In other languages, like C++, this would crash your program, but in Objective-C, invoking a method on nil returns a zero value. This greatly simplifies expressions, as it obviates the need to check for nil before doing anything: // For example, this expression... if (name != nil && [name isEqualToString:@"Steve"]) { ... } // ...can be simplified to: if ([name isEqualToString:@"steve"]) { ... } Wow, that is some bad…
That's not bad code, you must be sarcastic. I've never heard of the method isDifferentFromString: and besides, the method name suggests it's the same thing as isEqualToString: approached from the other side. Are you referring to the capitalization mistake in @"steve"? When using sarcasm keep in mind there's no tone in text ;)
I'm not saying the example is correct, I'm pointing out that "I've never heard of it" / "Works on my computer" are dangerous attitudes to have.