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

Classes, structures, and enumerations can all define instance methods懒构;

Classes, structures, and enumerations can also define type methods。 In Objective-C, classes are the only types that can define methods鸣剪,In Swift, you can choose whether to define a class, structure, or enumeration, and still have the flexibility to define methods on the type you create.

Instance Methods ? 實(shí)例方法

Class

class Counter{

? ? ? ? ?var count=0

? ? ? ? ?func increment() {

? ? ? ? ? ? ? ? ?count+=1

? ? ? ? ?}

? ? ? ? ?func increment(by amount:Int) {

? ? ? ? ? ? ? ? ?count +=amount

? ? ? ? ?}

? ? ? ? ?func reset() {

? ? ? ? ? ? ? ? ?count=0

? ? ? ? ?}

}

let counter=Counter() ??// the initial counter value is 0

counter.increment() ? ??// the counter's value is now 1

counter.increment(by:5) ? ??// the counter's value is now 6

counter.reset() ? ??// the counter's value is now 0

Struct

struct Point{

? ? ? ? ? var x=0.0,y=0.0

? ? ? ? ? func isToTheRightOf(x:Double) ->Bool{

? ? ? ? ? ? ? ? ? return self.x>x

? ? ? ? ? }

}

let somePoint=Point(x:4.0,y:5.0)

if somePoint.isToTheRightOf(x:1.0) ?{

? ? ? ?print("This point is to the right of the line where x == 1.0")

}

// Prints "This point is to the right of the line where x == 1.0"

Modifying Value Types from Within Instance Methods ?從實(shí)例方法中修改值類型

struct Point{

? ? ? ? ? var x=0.0,y=0.0

? ? ? ? ? mutating func moveBy(x deltaX:Double,y deltaY:Double) { //mutating關(guān)鍵字被添加到其定義徐钠,以使其能夠修改其屬性。

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?x +=deltaX

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?y +=deltaY

? ? ? ? ? }

}

var somePoint=Point(x:1.0,y:1.0)

somePoint.moveBy(x:2.0,y:3.0) // 改變實(shí)例屬性

print("The point is now at (\(somePoint.x),\(somePoint.y))")?

// Prints "The point is now at (3.0, 4.0)"

let fixedPoint=Point(x:3.0,y:3.0)

fixedPoint.moveBy(x:2.0,y:3.0)

// this will report an error 因?yàn)閒ixedPoint是個(gè)常量無(wú)法改變其實(shí)例屬性

Assigning to self Within a Mutating Method ?重新改變自己

struct Point{

? ? ? ? ? var x=0.0,y=0.0

? ? ? ? ? mutating func moveBy(x deltaX:Double,y deltaY:Double) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? self=Point(x:x+deltaX,y:y+deltaY) //實(shí)例對(duì)象調(diào)用實(shí)例方法改變對(duì)象本身

? ? ? ? ? }

}

enum TriStateSwitch{

? ? ? ? ? case off,low,high

? ? ? ? ? mutating func next() {

? ? ? ? ? ? ? ? ? ? ? ?switch self{

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? case .off:

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?self= .low

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? case .low:

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?self= .high

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? case .high:

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?self= .off

? ? ? ? ? ? ? ? ? ? ? ?}

? ? ? ? ? ?}

}

var ovenLight=TriStateSwitch.low

ovenLight.next() ? ? ?// ovenLight is now equal to .high

ovenLight.next() ? ? ??// ovenLight is now equal to .off

Type Methods ? 類方法

class SomeClass{

? ? ? ? ? class func someTypeMethod() {

? ? ? ? ? ? ? ? ? ? ? ? ? ? // type method implementation goes here

? ? ? ? ? }

}

SomeClass.someTypeMethod()

struct LevelTracker{

? ? ? ? ? static var highestUnlockedLevel=1

? ? ? ? ? var currentLevel=1

? ? ? ? ? static func unlock( _ level:Int) { //類方法? 操作的是類屬性highestUnlockedLevel

? ? ? ? ? ? ? ? ? ? ? ? ? ? if level>highestUnlockedLevel {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?highestUnlockedLevel=level

? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? }

? ? ? ? ? static func isUnlocked( _ level:Int)? -> Bool{ // 類方法 操作的是類屬性highestUnlockedLevel

? ? ? ? ? ? ? ? ? ? ? ? ? ? return level <= highestUnlockedLevel

? ? ? ? ? }

? ? ? ? ? @discardableResult ?//忽略返回值的代碼不一定是錯(cuò)誤

? ? ? ? ? mutating func advance(to level:Int) ->Bool{ // 可修改的實(shí)例方法 ? 操作的是實(shí)例屬性

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?if LevelTracker.isUnlocked(level) ?{ // 調(diào)用類方法

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?currentLevel=level

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?return true

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}else{

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?return false

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}

? ? ? ? ? }

}

class Player{

? ? ? ? ?var tracker = LevelTracker()

? ? ? ? ?let playerName:String

? ? ? ? ?func complete(level:Int) {

? ? ? ? ? ? ? ? ?LevelTracker.unlock(level+1) // 調(diào)用類方法

? ? ? ? ? ? ? ? ?tracker.advance(to:level+1) // 調(diào)用實(shí)例方法

? ? ? ? ?}

? ? ? ? ?init(name:String) { //屬性構(gòu)造器

? ? ? ? ? ? ? ? playerName=name

? ? ? ? ?}

}

var player=Player(name:"Argyrios")

player.complete(level:1)

print("highest unlocked level is now\(LevelTracker.highestUnlockedLevel)")

// Prints "highest unlocked level is now 2"

player=Player(name:"Beto")

if player.tracker.advance(to:6) ?{

? ? ? ? print("player is now on level 6")

}else{

? ? ? ? print("level 6 has not yet been unlocked")

}

// Prints "level 6 has not yet been unlocked"

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市她混,隨后出現(xiàn)的幾起案子联贩,更是在濱河造成了極大的恐慌漫仆,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,427評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件泪幌,死亡現(xiàn)場(chǎng)離奇詭異盲厌,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)祸泪,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門吗浩,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人没隘,你說(shuō)我怎么就攤上這事拓萌。” “怎么了升略?”我有些...
    開封第一講書人閱讀 165,747評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵微王,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我品嚣,道長(zhǎng)炕倘,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,939評(píng)論 1 295
  • 正文 為了忘掉前任翰撑,我火速辦了婚禮罩旋,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘眶诈。我一直安慰自己涨醋,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,955評(píng)論 6 392
  • 文/花漫 我一把揭開白布逝撬。 她就那樣靜靜地躺著浴骂,像睡著了一般。 火紅的嫁衣襯著肌膚如雪宪潮。 梳的紋絲不亂的頭發(fā)上溯警,一...
    開封第一講書人閱讀 51,737評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音狡相,去河邊找鬼梯轻。 笑死,一個(gè)胖子當(dāng)著我的面吹牛尽棕,可吹牛的內(nèi)容都是我干的喳挑。 我是一名探鬼主播,決...
    沈念sama閱讀 40,448評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼伊诵!你這毒婦竟也來(lái)了媚朦?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,352評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤日戈,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后孙乖,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體浙炼,經(jīng)...
    沈念sama閱讀 45,834評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,992評(píng)論 3 338
  • 正文 我和宋清朗相戀三年唯袄,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了弯屈。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,133評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡恋拷,死狀恐怖资厉,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情蔬顾,我是刑警寧澤宴偿,帶...
    沈念sama閱讀 35,815評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站诀豁,受9級(jí)特大地震影響窄刘,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜舷胜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,477評(píng)論 3 331
  • 文/蒙蒙 一娩践、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧烹骨,春花似錦翻伺、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至峦树,卻和暖如春未妹,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背空入。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工络它, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人歪赢。 一個(gè)月前我還...
    沈念sama閱讀 48,398評(píng)論 3 373
  • 正文 我出身青樓化戳,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子点楼,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,077評(píng)論 2 355

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

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問(wèn)題, 分享了一些自己做題目的經(jīng)驗(yàn)扫尖。 張土汪:刷leetcod...
    土汪閱讀 12,747評(píng)論 0 33
  • 上個(gè)世紀(jì)九十年代,西南地區(qū)掠廓,長(zhǎng)壽鎮(zhèn)换怖。 長(zhǎng)壽鎮(zhèn)自清朝起以長(zhǎng)壽聞名,生活在這里的每個(gè)村民都渴望著長(zhǎng)命百歲蟀瞧,并且以此信仰...
    我會(huì)開花你會(huì)嗎閱讀 454評(píng)論 0 0
  • 文字碼了又碼沉颂,刪了又刪,不知自己是想表達(dá)些什么悦污,亦或是詞不達(dá)意铸屉,亦或是心中無(wú)意,亦或是二者兼有切端。 時(shí)常懊悔...
    胡離憂閱讀 520評(píng)論 0 1
  • 親愛的自己Me: 已經(jīng)寫了9天的情書彻坛,這第10封信想寫給自已。 我的眼晴這二天到晚上8踏枣,9點(diǎn)就有了磕睡昌屉,但我還是要...
    一黍花園閱讀 149評(píng)論 2 5
  • 文/羅桑 今天突然興起,打算收拾一下那些閑置的書籍茵瀑。然后怠益,那個(gè)由塑膠封面包裹著的語(yǔ)文錯(cuò)題集便闖入了我的視野。 那枝...
    初學(xué)者羅桑閱讀 343評(píng)論 0 1