計算屬性由類倔约,結(jié)構(gòu)和枚舉提供。 存儲屬性僅由類和結(jié)構(gòu)提供崎岂。
Stored Properties ?存儲屬性
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
Stored Properties of Constant Structure Instances? 常量結(jié)構(gòu)實例的存儲屬性
let rangeOfFourItems = FixedLengthRange(firstValue:0,length:4)
// this range represents integer values 0, 1, 2, and 3
rangeOfFourItems.firstValue=6
// this will report an error, even though firstValue is a variable property 因為rangeOfFourItems這是一個常量 芳杏, 它的屬性值無法改變
Lazy Stored Properties ? ? 延遲存儲屬性
Note:您必須始終將lazy屬性聲明為變量(使用var關(guān)鍵字)栓撞,因為它的初始值可能無法在實例初始化完成后檢索。 常量屬性在初始化完成之前必須始終具有值赊豌,因此不能聲明為延遲扛或。
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
print(manager.importer.fileName) ? // 現(xiàn)在才實例化類里面的延遲屬性
// the DataImporter instance for the importer property has now been created
// Prints "data.txt"
Note:如果標(biāo)記有l(wèi)azy修飾符的屬性由多個線程同時訪問亿絮,并且該屬性尚未初始化告喊,則不能保證該屬性只被初始化一次。
Computed Properties ? ?計算屬性 ?
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現(xiàn)在是(5,5)square.origin是(0,0)square.size = (10.0,10.0) square.center是通過計算屬性的getter方法計算出來的
square.center=Point(x:15.0,y:15.0) // square.center現(xiàn)在是(15,15)square.origin是(10,10)square.size = (10.0,10.0)? square.origin是通過計算屬性的setter方法計算出來的
print("square.origin is now at (\(square.origin.x),\(square.origin.y))")
// Prints "square.origin is now at (10.0, 10.0)"?
Read-Only Computed Properties ? ? ?只讀計算屬性 ? ?那就把setter方法去掉
Property Observers 屬性觀察器
willSet is called just before the value is stored.
didSet is called immediately after the new value is stored.
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
Global and Local Variables ? 全局和局部變量
Type Properties ? 類屬性
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{
? ? ? ? ? ? ? ? ? ? return107
? ? ? ? ? }
}
訪問 和 設(shè)置 類屬性
print(SomeStructure.storedTypeProperty)
// Prints "Some value."
SomeStructure.storedTypeProperty="Another value."
print(SomeStructure.storedTypeProperty)
// Prints "Another value."
print(SomeEnumeration.computedTypeProperty)
// Prints "6"
print(SomeClass.computedTypeProperty)
// Prints "27"
struct AudioChannel{
? ? ? ?static let thresholdLevel=10
? ? ? ?static var maxInputLevelForAllChannels=0
? ? ? ?var currentLevel:Int=0{
? ? ? ? ? ? ? didSet{
? ? ? ? ? ? ? ? ? ? ? ?if currentLevel>AudioChannel.thresholdLevel { // 在本類里面調(diào)用類屬性
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?// cap the new audio level to the threshold level
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?currentLevel=AudioChannel.thresholdLevel
? ? ? ? ? ? ? ? ? ? ? ?}?
? ? ? ? ? ? ? ? ? ? ? ?if currentLevel>AudioChannel.maxInputLevelForAllChannels {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // store this as the new overall maximum input level
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?AudioChannel.maxInputLevelForAllChannels=currentLevel
? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? }
? ? ? ?}
}
var leftChannel=AudioChannel()
var rightChannel=AudioChannel()
leftChannel.currentLevel=7
print(leftChannel.currentLevel) ? ? ?// Prints "7"
print(AudioChannel.maxInputLevelForAllChannels) ? ? ? ?// Prints "7"
rightChannel.currentLevel=11
print(rightChannel.currentLevel) ? ? ? ?// Prints "10"
print(AudioChannel.maxInputLevelForAllChannels) ? ? ? ?// Prints "10"