Re: Objective-C Question
Re: Objective-C Question
- Subject: Re: Objective-C Question
- From: Jean-Daniel Dupas <email@hidden>
- Date: Tue, 12 Mar 2013 20:38:50 +0100
Le 12 mars 2013 à 20:15, Dave <email@hidden> a écrit :
>
> On 12 Mar 2013, at 18:50, Jens Alfke wrote:
>
>>
>> On Mar 12, 2013, at 9:44 AM, John McCall <email@hidden> wrote:
>>
>>> However, that wouldn't be an idiomatic implementation. The usual expectation is that allocating methods, like +new methods, construct an object *of type self* (at least).
>>
>> Are you just quibbling about the prefix “new-“? The specific method +new is expected to create a new instance of the receiving class, yes. There may be a convention (I can’t remember?) that class methods prefixed “new-” are intended to do the same.
>
> I'm not sure if its a convention, but it does make sense, for instance, I've been code like this before.
>
> -(NSDictionary) newDictForSomethingWithInfo:(Info*) theInfo
> {
> // returns an dictionary allocated and initialized
> }
>
> somewhere else in the same class:
>
> myDict = [self newDictForSomethingWithInfo:myInfo];
>
> Which keeps the compiler/analyzer happy in non-ARC and works ok with ARC enabled. If this isn't a convention then it should be IMO unless there is another prefix used? allocDict would work, but this is as bad or as good as new in this respect.
>
According to the clang manual: http://clang.llvm.org/docs/LanguageExtensions.html#related-result-types
The related result type can also be inferred for some methods. To determine whether a method has an inferred related result type, the first word in the camel-case selector (e.g., “init” in “initWithObjects”) is considered, and the method will have a related result type if its return type is compatible with the type of its class and if:
• the first word is “alloc” or “new”, and the method is a class method, or
• the first word is “autorelease”, “init”, “retain”, or “self”, and the method is an instance method.
So new prefixed class method should return an instance of the calling class, but instance methods are just required to returned a retained object of any kind.
-- Jean-Daniel
_______________________________________________
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