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.