在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
普通属性用var和let即可,本文不做详述 1.延迟存储属性 延迟存储属性是指当第一次被调用的时候才会计算其初始值的属性。在属性声明前使用 class DataImporter { /* DataImporter 是一个将外部文件中的数据导入的类。 这个类的初始化会消耗不少时间。 */ var fileName = "data.txt" // 这是提供数据导入功能 } class DataManager { @lazy var importer = DataImporter() var data = String[]() // 这是提供数据管理功能 } let manager = DataManager() manager.data += "Some data" manager.data += "Some more data" // DataImporter 实例的 importer 属性还没有被创建
2.计算属性 计算属性没有存储数据,结果由计算得到。swift提供一个 getter 来获取值,一个可选的 setter 来间接设置其他属性或变量的值,必须使用var关键字定义计算属性,下面例子中的center属性既是计算属性 struct Point { var x = 0.0, y = 0.0 } struct Size { var width = 0.0, height = 0.0 } struct Rect { var origin = Point() var size = Size() var center: Point { get { let centerX = origin.x + (size.width / 2) let centerY = origin.y + (size.height / 2) return Point(x: centerX, y: centerY) } set(newCenter) { origin.x = newCenter.x - (size.width / 2) origin.y = newCenter.y - (size.height / 2) } } } var square = Rect(origin: Point(x: 0.0, y: 0.0), size: Size(width: 10.0, height: 10.0)) let initialSquareCenter = square.center square.center = Point(x: 15.0, y: 15.0) println("square.origin is now at (\(square.origin.x), \(square.origin.y))") // 输出 "square.origin is now at (10.0, 10.0)” 如果计算属性的 setter 没有定义表示新值的参数名,则可以使用默认名称 struct AlternativeRect { var origin = Point() var size = Size() var center: Point { get { let centerX = origin.x + (size.width / 2) let centerY = origin.y + (size.height / 2) return Point(x: centerX, y: centerY) } set { origin.x = newValue.x - (size.width / 2) origin.y = newValue.y - (size.height / 2) } } }
3.属性监视器(willSet & didSet)
class StepCounter { var totalSteps: Int = 0 { willSet(newTotalSteps) { println("About to set totalSteps to \(newTotalSteps)") } didSet { if totalSteps > oldValue { println("Added \(totalSteps - oldValue) steps") } } } } let stepCounter = StepCounter() stepCounter.totalSteps = 200 // About to set totalSteps to 200 // Added 200 steps stepCounter.totalSteps = 360 // About to set totalSteps to 360 // Added 160 steps stepCounter.totalSteps = 896 // About to set totalSteps to 896 // Added 536 steps
在didSet中可以修改class对象中的属性值,如果修改的是自己,不会再次触发属性监视事件;如果是其他属性,则会调用其他属性的监听事件 class StepCounter { var totalSteps: Int = 0 { willSet(newTotalSteps) { println("About to set totalSteps to \(newTotalSteps)") } didSet { if totalSteps > oldValue { println("Added \(totalSteps - oldValue) steps") xyz = 110 totalSteps = 0 } } } var xy:Int = 1 var xyz:Int = 0{ didSet { println("ChangedValue)") } } } let stepCounter = StepCounter() stepCounter.totalSteps = 200 println(stepCounter.totalSteps) println(stepCounter.xyz) //About to set totalSteps to 200 //Added 200 steps //ChangedValue) //0 //110
类似于java中的静态属性 使用关键字 struct SomeStructure { static var storedTypeProperty = "Some value." static var computedTypeProperty: Int { return 10 } } enum SomeEnumeration { static var storedTypeProperty = "Some value." static var computedTypeProperty: Int { return 5 } } class SomeClass { class var computedTypeProperty: Int { return 3 } } println(SomeClass.computedTypeProperty) // 输出 "3" println(SomeStructure.storedTypeProperty) // 输出 "Some value." SomeStructure.storedTypeProperty = "Another value." println(SomeStructure.storedTypeProperty) // 输出 "Another value.”
|
请发表评论