property有三种,分别是stored property,computed property, type property. stored property可以用在class和structure中,computed property可以用在class、structure和enumeration中。
stored property
stored property 跟c++/c#里面的成员变量很相似。它可以用var和let两种修饰方式,分别表示变量和常量。对于变量,还可以提供lazy关键字来给property提供懒加载功能。例如:
struct FixedLengthRange {
var firstValue: Int
let length: Int
}
var rangeOfThreeItems = FixedLengthRange(firstValue: 0, length: 3)
// the range represents integer values 0, 1, and 2
rangeOfThreeItems.firstValue = 6
// the range now represents integer values 6, 7, and 8
摘录来自: Apple Inc. “The Swift Programming Language (Swift 3)”。 iBooks.
关于lazy初始化property的例子:
class DataImporter {
/*
DataImporter is a class to import data from an external file.
The class is assumed to take a non-trivial amount of time to initialize.
*/
var fileName = "data.txt"
// the DataImporter class would provide data importing functionality here
}
class DataManager {
lazy var importer = DataImporter()
var data = [String]()
// the DataManager class would provide data management functionality here
}
let manager = DataManager()
manager.data.append("Some data")
manager.data.append("Some more data")
// the DataImporter instance for the importer property has not yet been created
摘录来自: Apple Inc. “The Swift Programming Language (Swift 3)”。 iBooks.
computed property
computed property的意思就是本身并不直接存储值,但是可以通过get和set方法来间接获取或者改变其他property的值。看例子:
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)
}
}
}
摘录来自: Apple Inc. “The Swift Programming Language (Swift 3)”。 iBooks.
在这里,set方法是可以有一个参数的,参数的值就是要设置的新值。如果没有参数,则可以用newValue来默认指代。
如果只有get方法,也就是说这个computed property 是一个只读property,则get方法和大括号都可以省略,例如:
struct AlternativeRect {
var origin = Point()
var size = Size()
var center: Point {
let centerX = origin.x + (size.width / 2)
let centerY = origin.y + (size.height / 2)
return Point(x: centerX, y: centerY)
}
}
type property
type property 类似于c++/c#中的静态成员变量。对于struct和enumeration类型的用static定义,对于class类型的,可以使用class替代static,来让子类可以复写父类的实现。
struct SomeStructure {
static var storedTypeProperty = "Some value."
static var computedTypeProperty: Int {
return 1
}
}
enum SomeEnumeration {
static var storedTypeProperty = "Some value."
static var computedTypeProperty: Int {
return 6
}
}
class SomeClass {
static var storedTypeProperty = "Some value."
static var computedTypeProperty: Int {
return 27
}
class var overrideableComputedTypeProperty: Int {
return 107
}
}
摘录来自: Apple Inc. “The Swift Programming Language (Swift 3)”。 iBooks.
stored type property必须在定义的时候给一个默认值,因为在初始化的时候没有一个initializer可以给它赋值。stored type property会在初次被使用的时候懒加载,即使有多个线程同时访问它,也可以保证只被初始化一次,而且它们不需要加lazy关键字。
property observers
跟oc类似,property赋值前后有两个方法可以提前知晓和确认这种变化,willSet会在赋值前调用,didSet会在赋值后立即调用。
class StepCounter {
var totalSteps: Int = 0 {
willSet(newTotalSteps) {
print("About to set totalSteps to \(newTotalSteps)")
}
didSet {
if totalSteps > oldValue {
print("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
摘录来自: Apple Inc. “The Swift Programming Language (Swift 3)”。 iBooks.