• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Declaring Variables - Setting to nil VS Not Setting?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

  • Follow-Ups:
    • Re: Declaring Variables - Setting to nil VS Not Setting?
      • From: Chris Tracewell <email@hidden>
References: 
 >Declaring Variables - Setting to nil VS Not Setting? (From: Chris Tracewell <email@hidden>)

  • Prev by Date: Re: Declaring Variables - Setting to nil VS Not Setting?
  • Next by Date: Re: SMJobBless
  • Previous by thread: Re: Declaring Variables - Setting to nil VS Not Setting?
  • Next by thread: Re: Declaring Variables - Setting to nil VS Not Setting?
  • Index(es):
    • Date
    • Thread