Re: Objective-C Question
Re: Objective-C Question
- Subject: Re: Objective-C Question
- From: Dave <email@hidden>
- Date: Tue, 12 Mar 2013 19:53:42 +0000
On 12 Mar 2013, at 19:38, Jean-Daniel Dupas wrote:
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 if its return type is compatible with the type
of its class and if:
That's true (incompatible result type) in this case and in most
cases I've seen, so ok to use newDict in this case.
• 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.
Well, that's one rule that sucks and I've seen it disobeyed many
times. If that is the case, then how do you signal to the compiler/
analyzer that you are returning a retained object? Unless there is
another prefix to use, they I am going to continue ignore it too!
All the Best
Dave
PS.
This lets eveyone off the hook:
if its return type is compatible with the type of its class and if:
Just seen this:
That's true (incompatible result type) in this case and in most
cases I've seen, so ok to use newDict in this case.
_______________________________________________
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