self.foo = nil和[self setFoo:nil]有什么区别? [英] What's the difference between self.foo=nil and [self setFoo:nil]?

查看:55
本文介绍了self.foo = nil和[self setFoo:nil]有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在xcode中,创建UIViewController子类后,在viewDidUnload方法中,xcode添加了注释:

In the xcode, after you creating a UIViewController subclass, in the viewDidUnload method, there is a comment added by xcode:

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

在这里,xcode建议我们使用self.myOutlet = nil释放对象.

Here, xcode recommends us to use self.myOutlet=nil to release object.

但是在xcode4中,有一个很酷的功能:您可以将Interface Builder出口拖到其所有者的头文件中,然后xcode将在viewDidUnload方法中自动创建IBOutlet对象和相关的发布代码.

But in xcode4, there is a cool feature: you can just drag a Interface Builder outlet to its owner's header file, then xcode will automatically create the IBOutlet object and relevant release code in viewDidUnload method.

问题在于上述情况,它生成的代码是这样的:

The problem is in above scenario, it generated code is something like this:

- (void)viewDidUnload {
    [super viewDidUnload];
    [self setFoo:nil];
}

我提到"self.foo = nil;"替换为"[self setFoo:nil];".

I mention that the "self.foo = nil;" is replaced by "[self setFoo:nil];".

有人知道其中的区别吗?如果没有区别,为什么xcode4会对其进行更改?

Does anybody know the difference? If there is no difference, why xcode4 changes it?

谢谢.

推荐答案

在可执行文件中,两种语法之间没有区别.我能想到的Xcode明确使用setter方法的唯一技术原因是与与不支持点语法的Objective-C 2.0以前的代码兼容.我不知道为什么Xcode需要支持这种兼容性.可能Xcode中生成set-to-nil表达式的部分早于Objective-C 2.0.

In the executable, there is no difference between the two syntaxes. The only technical reason I can think of for Xcode to use the setter method explicitly is for compatibility with pre-Objective-C 2.0 code, which didn't support dot-syntax. I don't know why Xcode would need to support that compatibility; maybe the part of Xcode that generates the set-to-nil expression is older than Objective-C 2.0.

或者也许编写Xcode的那部分人根本不喜欢点语法.首次引入点语法时,很多人不喜欢它.

Or maybe the person who wrote that part of Xcode simply doesn't like dot-syntax. When dot-syntax was first introduced, a lot of people didn't like it.

http://weblog.bignerdranch.com/?p=83
http://www.cimgf.com/2008/07/08/a-case-against-dot-syntax/
http://cocoawithlove.com/2008/08/in-defense-of-objective-c-20-properties.html

这篇关于self.foo = nil和[self setFoo:nil]有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆