开发者

Objective-C and autorelease on return

开发者 https://www.devze.com 2023-02-15 21:46 出处:网络
Are the two me开发者_开发问答thods - (id) myFirstMethod { NSObject* anObject = [[NSObject alloc] init];

Are the two me开发者_开发问答thods

- (id) myFirstMethod
{
  NSObject* anObject = [[NSObject alloc] init];
  [anObject autorelease];
  return anObject;
}

- (id) mySecondMethod
{
  NSObject* anObject = [[NSObject alloc] init];
  return [anObject autorelease];
}

identical?


Yes the both methods are identical. Whenever you write a nested function or same code in multiple lines, it is all the same after compilation.


You could do everything in the return:

- (id) myThirdMethod {
    return [[[NSObject alloc]init]autorelease];
    }


Yes they are identical, but all they do is to cause a compiler error.

[anObject] is not allowed. And NSObject anObject is also invalid syntax.

0

精彩评论

暂无评论...
验证码 换一张
取 消