ViewController.swift
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let p = Person() print(p.title) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
Person.swift
import UIKit class Person: NSObject { var _name: String? // var name: String? { // set { // _name = newValue // } // get { // return _name // } // } //只读属性 又叫做 计算性属性 //一旦只实实现了 get方法 默认的隐含形式 添加的set方法 就会失效 //并不会分配内存空间 var name: String? { get { return "zhangsan" } } //并不需要分配内存空间 // var title: String? { // get { // return "Boos" + (name ?? "") // } // } //简写方式 var title: String? { return "Boos" + (name ?? "") } var model: NSObject? { didSet { //cell绑定数据 } } }原文链接:https://www.f2er.com/swift/324961.html