I’m working someone else’s code. I’ve never encountered something like this before:
return [[[NSObject alloc] init] autorelease];
Can someone tell me what this means and why someone would use it? Just to be clear, I’m not asking about the autorelease portion. I would have the same question about this code:
-(id)someMethod
{
lots of lines of code
...
return [[NSObject alloc]init];
}
It is just returning an autoreleased object for convenience. This means that when you use the function you do not have to append
autoreleasemessage to it generally speaking. You may always want certain objects to be autoreleased.As an example, many of the included convenience (“factory”) methods in Objective-C return an autoreleased object. You are probably familiar with
[NSString stringWithFormat:__FORMAT__]which returns an autoreleasedNSString. Take a look at http://memo.tv/archive/memory_management_with_objective_c_cocoa_iphoneAs an example of why a function might return an object, consider a synchronous URL request, where you may or may not care about a response, or a function like the following: