Live data from Hacker News

nil, Nil, NULL, NSNull

nshipster.com

1–10 of 66 posts

Re: nil, Nil, NULL, NSNull

#2
Good 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 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
> 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, bad code. Consider this snippet instead:

    if ([name isDifferentFromString:@"bob"]) { ... }

Re: nil, Nil, NULL, NSNull

#4
Also 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

#5

Also 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.

This is no longer true when using ARC.

Re: nil, Nil, NULL, NSNull

#6
post #3

> 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 seems like a naming decision, I think it'd make more sense to have an equals selector than a not equals selector in objective c. Plus isEqualToString: is already part of NSString

Re: nil, Nil, NULL, NSNull

#7

Also 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.

This is no longer true when using ARC. See the "Stack Variables Are Initialized with nil" section of http://developer.apple.com/library/ios/#releasenotes/Objecti...

"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

#8
post #2

Good 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…

To clarify: I mean to say that 0 represents "nothing" in C in the same way that it does in math. This is one of the things I love about C--the ability to exploit the properties of numbers directly, rather than needing to build abstractions around them. Bitmasks, for example, are one of my favorite things ever.

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
post #3

> 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 ;)

Re: nil, Nil, NULL, NSNull

#10
post #9
post #3

> 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 ;)

Well, if you've never heard of it then it cannot exist and that invalidates his example, right?

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.

Post reply on HN