self.property 或 属性 在 Swift

self.property or property in Swift

在 Swift 方法中,您可以使用 self.property 或简单地 property.

访问 属性

有什么不同吗?其中之一是首选吗?是否有 Apple 发布的风格指南需要其中之一?

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. You use the self property to distinguish between the parameter name and the property name.

参考:The Swift Programming Guide 来自 Apple。