• 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: Arc and **
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Arc and **


  • Subject: Re: Arc and **
  • From: John McCall <email@hidden>
  • Date: Wed, 05 Dec 2012 15:44:36 -0800

On Dec 4, 2012, at 10:44 PM, Gerriet M. Denkmann <email@hidden> wrote:
> On 5 Dec 2012, at 12:59, Greg Parker <email@hidden> wrote:
>> On Dec 4, 2012, at 8:56 PM, Gerriet M. Denkmann <email@hidden> wrote:
>>> I have (using Arc) a method which works fine:
>>> NSString *explanation;
>>> [ self doSomeThingAndExplain: &explanation ];
>>>
>>> Now I decided that sometimes I don't need this explanation. So I changed it to:
>>>
>>> NSString **explanatioP = urgent ? NULL : &explanation; // <-- "no explicit ownership...
>>> [ self doSomeThingAndExplain: explanatioP];	//	passing address of non-local object...
>>>
>>> But now the compiler gets really upset (errors written as comments above).
>>>
>>> What Arc-magic is needed to get this to compile?
>>
>> You need to add explicit ownership to your ** declarations. ARC needs to know whether explanationP points to to a strong variable or a weak variable or an autoreleasing variable.
>>
>> This is one way:
>>   -(void) doSomethingAndExplain:(__strong NSString **)explanationP;
>>
>>   __strong NSString **explanationP = urgent ? NULL : &explanation;
>>   [self doSomethingAndExplain:explanationP];
>>
>> This is another way:
>>   -(void) doSomethingAndExplain:(NSString **)explanationP;
>>   // NSString** parameter is implicitly __autoreleasing NSString **
>>
>>   __autoreleasing NSString **explanationP = urgent ? NULL : &explanation;
>>   [self doSomethingAndExplain:explanationP];
>>
>> The first version may have better performance because it avoids the autorelease pool.
>
> Another way (I don't know how efficient it is though) is:
> [self doSomethingAndExplain: urgent ? NULL : &explanation ];
>
> Does this use  autorelease pool ?

Yes.  By default, an argument of type id* is assumed to be __autoreleasing id *,
because that's the common convention for out-parameters in Cocoa.

It is generally more efficient to make your out-parameters __strong id*, but
of course that requires a bit more work from MRC callers, which is why it's
not the standard convention.

John.
_______________________________________________

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

References: 
 >Arc and ** (From: "Gerriet M. Denkmann" <email@hidden>)
 >Re: Arc and ** (From: Greg Parker <email@hidden>)
 >Re: Arc and ** (From: "Gerriet M. Denkmann" <email@hidden>)

  • Prev by Date: Re: Object not being drawn in Cmd + N. Why? SOLVED
  • Next by Date: Re: Cascading windows
  • Previous by thread: Re: Arc and **
  • Next by thread: Cocoaheads Lake Forest meets tonight
  • Index(es):
    • Date
    • Thread