Re: Declaring Variables - Setting to nil VS Not Setting?
Re: Declaring Variables - Setting to nil VS Not Setting?
- Subject: Re: Declaring Variables - Setting to nil VS Not Setting?
- From: David Duncan <email@hidden>
- Date: Thu, 26 Apr 2012 13:20:43 -0700
On Apr 26, 2012, at 1:00 PM, Chris Tracewell wrote:
> I usually use the sample in line 1 when declaring vars inside my methods.
>
> NSString *theString = [NSString string];
If you are going to assign another value to 'theString' before you use it, then doing this is pointless.
> NSString *theString = nil;
> NSString *theString;
>
> I thought lines 2 and 3 were the same thing. I was wrong. What is the difference and when do you use the style shown in line 3?
In C unless you explicitly initialize a local variable its value is undefined, and Objective-C inherits this behavior.
I believe however that under ARC these lines are equivalent (at least thats my reading of section 4.2 on initialization at <http://clang.llvm.org/docs/AutomaticReferenceCounting.html>) but given your comments I suspect you are not using ARC.
--
David Duncan
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden