我只是想知道这两种定义变量的形式是否彼此不同.或者应该在某些特殊情况下使用.
var string1: String { return "ok" } var string2: String = "ok"
解决方法
第一种方法叫做
computed property:
Classes,structures,and enumerations can define computed properties,which do not actually store a value. Instead,they provide a getter and an optional setter to retrieve and set other properties and values indirectly.
您使用的表单只为string1提供了一个getter,使其成为只读属性.
第二种方法通常是stored property:
A stored property is a constant or variable that is stored as part of an instance of a particular class or structure. Stored properties can be either variable stored properties (introduced by the var keyword) or constant stored properties (introduced by the let keyword).
根据具体情况应该使用哪个.但有时只能引入计算属性(例如,如果您提供扩展名).
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。