Swift 3.0 學(xué)習(xí)筆記之Properties

計算屬性由類,結(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:如果標記有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"

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1,關(guān)于新版健身計劃 今天是我踐行新的健身計劃的第10天,從上周一(4月3日)開始,每周一到周六,周日休息,今天是...
    周書恒閱讀 198評論 2 2
  • 知識給人自信! 學(xué)習(xí)加深對知識的認識和理解! 學(xué)習(xí)提供更寬廣的思路! 學(xué)習(xí)跟大師交流,更加走進大師的世界! 有時候...
    烏龜?shù)穆?/span>閱讀 359評論 0 0
  • 摘自51testing軟件測試網(wǎng),原文鏈接自動化測試用例設(shè)計原則。 很多公司在實施自動化測試的過程中,往往會把所有...
    Leo_0626閱讀 4,516評論 0 11
  • 1. 午后,我不能抑制自己,放下平板,一臉傲嬌地抽出《妖精的小孩》這本書,一路小跑到一樓,坐在沙發(fā)上就津津有味地讀...
    中二火華閱讀 465評論 0 1
  • 小序: 萬千世界或許有很多如果,有很多想得卻得不到的,但是那又如何,得不到只是自己心傷罷了在別人眼里可能就是你不配...
    usedtodosth閱讀 265評論 0 0

友情鏈接更多精彩內(nèi)容