self.view.addSubview 和 view.addSubview 之间的区别
Difference between self.view.addSubview and view.addSubview
我在 swift 中完成了很多编码,并且更喜欢以编程方式做很多事情,我想知道这两者之间的区别是什么:
self.view.addSubview(someNewView)
view.addSubview(someNewView)
它们似乎都有效。出于某种原因更好吗?他们真的那么不同吗?
如果这是一个愚蠢的问题或已经回答,可以将其删除。只是一个想法。
没有真正的区别,尽管您可能会更频繁地看到以前 Objective-C 开发人员使用 self
。来自文档:
In practice, you don’t need to write self in your code very often. If
you don’t explicitly write self, Swift assumes that you are referring
to a property or method of the current instance whenever you use a
known property or method name within a method.
...
The main exception to this rule occurs when a parameter name for an instance method has the same name as a property of that instance. In this situation, the parameter name takes precedence, and it becomes necessary to refer to the property in a more qualified way.
我在 swift 中完成了很多编码,并且更喜欢以编程方式做很多事情,我想知道这两者之间的区别是什么:
self.view.addSubview(someNewView)
view.addSubview(someNewView)
它们似乎都有效。出于某种原因更好吗?他们真的那么不同吗?
如果这是一个愚蠢的问题或已经回答,可以将其删除。只是一个想法。
没有真正的区别,尽管您可能会更频繁地看到以前 Objective-C 开发人员使用 self
。来自文档:
In practice, you don’t need to write self in your code very often. If you don’t explicitly write self, Swift assumes that you are referring to a property or method of the current instance whenever you use a known property or method name within a method.
...
The main exception to this rule occurs when a parameter name for an instance method has the same name as a property of that instance. In this situation, the parameter name takes precedence, and it becomes necessary to refer to the property in a more qualified way.