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:如果標(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"

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末派昧,一起剝皮案震驚了整個濱河市黔姜,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌蒂萎,老刑警劉巖秆吵,帶你破解...
    沈念sama閱讀 222,252評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異五慈,居然都是意外死亡纳寂,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,886評論 3 399
  • 文/潘曉璐 我一進店門泻拦,熙熙樓的掌柜王于貴愁眉苦臉地迎上來毙芜,“玉大人,你說我怎么就攤上這事争拐∫钢啵” “怎么了?”我有些...
    開封第一講書人閱讀 168,814評論 0 361
  • 文/不壞的土叔 我叫張陵架曹,是天一觀的道長隘冲。 經(jīng)常有香客問我,道長绑雄,這世上最難降的妖魔是什么展辞? 我笑而不...
    開封第一講書人閱讀 59,869評論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮万牺,結(jié)果婚禮上罗珍,老公的妹妹穿的比我還像新娘洽腺。我一直安慰自己,他們只是感情好靡砌,可當(dāng)我...
    茶點故事閱讀 68,888評論 6 398
  • 文/花漫 我一把揭開白布已脓。 她就那樣靜靜地躺著,像睡著了一般通殃。 火紅的嫁衣襯著肌膚如雪度液。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,475評論 1 312
  • 那天画舌,我揣著相機與錄音堕担,去河邊找鬼。 笑死曲聂,一個胖子當(dāng)著我的面吹牛霹购,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播朋腋,決...
    沈念sama閱讀 41,010評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼齐疙,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了旭咽?” 一聲冷哼從身側(cè)響起贞奋,我...
    開封第一講書人閱讀 39,924評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎穷绵,沒想到半個月后轿塔,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,469評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡仲墨,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,552評論 3 342
  • 正文 我和宋清朗相戀三年勾缭,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片目养。...
    茶點故事閱讀 40,680評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡俩由,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出癌蚁,到底是詐尸還是另有隱情幻梯,我是刑警寧澤,帶...
    沈念sama閱讀 36,362評論 5 351
  • 正文 年R本政府宣布匈勋,位于F島的核電站礼旅,受9級特大地震影響膳叨,放射性物質(zhì)發(fā)生泄漏洽洁。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,037評論 3 335
  • 文/蒙蒙 一菲嘴、第九天 我趴在偏房一處隱蔽的房頂上張望饿自。 院中可真熱鬧汰翠,春花似錦、人聲如沸昭雌。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,519評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽烛卧。三九已至佛纫,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間总放,已是汗流浹背呈宇。 一陣腳步聲響...
    開封第一講書人閱讀 33,621評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留局雄,地道東北人甥啄。 一個月前我還...
    沈念sama閱讀 49,099評論 3 378
  • 正文 我出身青樓,卻偏偏與公主長得像炬搭,于是被迫代替她去往敵國和親蜈漓。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,691評論 2 361

推薦閱讀更多精彩內(nèi)容

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