Z S
Z S

Reputation: 7511

objective-c: initialize variable to nil

Does anyone know if the following code could be problematic:

NSString *addchar = nil;

if (case1)
addChar = [[firstname substringToIndex:1] capitalizedString];
else
addChar = [[fullname substringToIndex:1] capitalizedString];

Assume that firstname and fullname aren't null or empty. Does initializing the NSString object and setting it to 'nil' cause some possible problem? It seems to cause my app to freeze, but only for a very few users, and only those users, but it doesn't have anything to do with different input strings or empty strings. So I'm trying to isolate the problem, but I don't know the difference between

NSString *addChar;

and

NSString *addChar = nil;

Thanks.

Upvotes: 3

Views: 4387

Answers (5)

MonsieurDart
MonsieurDart

Reputation: 6035

From now on, when you use ARC, the strong, weak, and autoreleasing stack variables are initialized to nil:

https://developer.apple.com/library/ios/releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html#//apple_ref/doc/uid/TP40011226-CH1-SW5

Additionally, since dawn of times, in ObjC the instance variables are initialized to zero:

Are instance variables set to nil by default in Objective-C?

Upvotes: 0

Romain
Romain

Reputation: 12819

There is no difference here since you don't read addChar in your code. Moreover, the compiler may sometimes initialize addChar to nil for you if you don't do it explicitly (depending on the scope of addChar's declaration).

See related question at this place.

Upvotes: 1

stefanB
stefanB

Reputation: 79790

You should always initialize to nil if variable is not initialized otherwise.

You can send messages to nil they will be ignored.

NSString * str = nil;
NSLog(@"%@", [str description]);

Outputs:

2009-12-15 08:59:03.352 x[11775] (nil)

Of course I don't need to call description explicitly, I'm just demonstrating call to nil.

Upvotes: 3

Steven Schlansker
Steven Schlansker

Reputation: 38526

Without the nil initializer, in some cases your variable may be initialized with garbage (whatever was in the memory space previously). There are specific rules regarding which types of variables (scope-based, static storage, etc.) are nil-initialized automatically for you, but I've always found that it's easier to explicitly initialize all variables instead of memorizing those rules.

That said, because both branches of your if statement clobber any previous value of addChar, there should not be any case in which you can see an invalid value. But it's certainly not hurting anything to explicitly initialize to nil, and if a future maintainer comes along and changes the code paths you might find that the initializer saves you!

Upvotes: 6

Joshua Nozzi
Joshua Nozzi

Reputation: 61228

Either form is perfectly acceptable. The problem you're having is elsewhere. I recommend doing some code profiling with Instruments to figure out where this issue is occurring.

Upvotes: 7

Related Questions