gpt4 book ai didi

ios - self.view.addSubview 和 view.addSubview 的区别

转载 作者:行者123 更新时间:2023-11-28 10:01:48 32 4
gpt4 key购买 nike

我已经用 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.

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Methods.html

关于ios - self.view.addSubview 和 view.addSubview 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27752535/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com