Hacker News new | ask | show | jobs
by seanalltogether 4913 days ago
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.
4 comments

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.

That's a rule of C; stack vars are not initialized. Same thing would happen with an int.

Member variables of a class are set to zero by (init or alloc? can't remember.)

And, if you don’t or can’t use ARC, turn on -Wuninitialized or use the static analyzer (clang warns on uninitialized values only with -Wall).
This is no longer true when using ARC.