初探swift語言的學習筆記七(swift 的關健詞)


原文地址:http://blog.csdn.net/fengsh998/article/details/32133809
轉載請註明出處



每一種語言都有相應的關鍵詞,每一個關鍵詞都有他獨特的做用,來看看swfit中的關鍵詞: swift

    關鍵詞: app

用來聲明的: ide

「 class, deinit, enum, extension, func, import, init, let, protocol, static, struct, subscript, typealias, var.」 函數

用於子句的: 學習

「 break, case, continue, default, do, else, fallthrough, if, in, for, return, switch, where, while.」 測試

表達式和類型的: spa

「 as, dynamicType, is, new, super, self, __COLUMN__, __FILE__, __FUNCTION__, __LINE__」 .net

//特殊語境使用的: code

「didSet, get, inout, mutating, override, set, unowned, unowned(safe), unowned(unsafe), weak , willSet」 對象


class

用來定義一個類,相信你們並不陌生。

若是定義一個汽車類

[cpp]  view plain  copy
  在CODE上查看代碼片 派生到個人代碼片
  1. class Car  
  2. {  
  3.       init()  
  4.       {  
  5.             //to do init something.  
  6.        }  
  7. }  

init

相對於類的構造方法的修飾。


deinit

相對於類的釋構方法的修飾。

對於類的構造和釋構在swift 中須要使用關鍵詞來修飾,而不少高級語言並不須要特別的指定,便C++ 只須要類名與構造函數名相同就能夠,不須要額外的關鍵詞。


enum

 枚舉類型的聲明,這個與不少語方都相通。


extension

擴展,有點像oc中的categories 。

Swift 中的能夠擴展如下幾個:
添加計算型屬性和計算靜態屬性
定義實例方法和類型方法
提供新的構造器
定義下標
定義和使用新的嵌套類型
使一個已有類型符合某個接口


以下面擴展字符串:

[cpp]  view plain  copy
  在CODE上查看代碼片 派生到個人代碼片
  1. extension String{  
  2.     struct _Dummy {  
  3.         var idxVal: Int  
  4.         var _padding: Int  
  5.         var _padding2: Int  
  6.         var _padding3: Int  
  7.     }  
  8.     //過慮出數字  
  9.     func fitlerCharater() -> String  
  10.     {  
  11.         var numberstr : String = ""  
  12.         for character in self  
  13.         {  
  14.             let s :String = String(character)  
  15.               
  16.             //println(s.toInt())  
  17.             if let hs = s.toInt()  
  18.             {  
  19.                 numberstr += character  
  20.             }  
  21.         }  
  22.         return numberstr  
  23.     }  
  24.       
  25.     //擴展使用下標訪問  
  26.     subscript (i: Int) -> Character {  
  27.         var dummy: _Dummy = reinterpretCast(i >= 0 ? self.startIndex : self.endIndex)  
  28.             dummy.idxVal += i  
  29.             let idx: String.Index = reinterpretCast(dummy)  
  30.             return self[idx]  
  31.     }  
  32.       
  33.     //擴展使用Range訪問  
  34.     subscript (subRange: Range<Int>) -> String {  
  35.         var start: _Dummy = reinterpretCast(self.startIndex)  
  36.             var end = start  
  37.             start.idxVal = subRange._startIndex  
  38.             end.idxVal = subRange._endIndex  
  39.             let startIndex: String.Index = reinterpretCast(start)  
  40.             let endIndex: String.Index = reinterpretCast(end)  
  41.             return self[startIndex..endIndex]  
  42.     }  
  43. }  

測試:
[cpp]  view plain  copy
  在CODE上查看代碼片 派生到個人代碼片
  1. func testExtension()  
  2. {  
  3.     var str : String = "1234ab5國6cd7中8i90"  
  4.     println(str.fitlerCharater())  
  5.       
  6.     let china: String = "china operating system public to 世界"  
  7.     println("使用下標索引訪問第13個字符 \(china[13])")  
  8.     println("使用負號下標即變爲從右往左訪問字符 \(china[-1])")  
  9.     println("使用負號下標即變爲從右往左訪問字符 \(china[-2])")  
  10.     println("使用下標Range來訪問範圍 \(china[2...6])")  
  11.     dump(china[1..5], name: "china[1:4]")              //使用dump輸出  
  12.     dump(china[10...13], name: "china[10:13]")  
  13. }  

輸出:
[cpp]  view plain  copy
  在CODE上查看代碼片 派生到個人代碼片
  1. 1234567890  
  2. 使用下標索引訪問第13個字符 n  
  3. 使用負號下標即變爲從右往左訪問字符 界  
  4. 使用負號下標即變爲從右往左訪問字符 世  
  5. 使用下標Range來訪問範圍 ina o  
  6. - china[1:4]: hina  
  7. - china[10:13]: atin  

func

 用來修飾函數的關鍵詞。

import

 導入頭文件,相信你們都不陌生,但在swift 中好像被用來導入包,如import UIKit。 由於swift中沒有了頭文件的概念。

let

用來修改某一常量的關鍵詞。像const 限定差很少

var

用來聲明變量。

protocol

協議,也有稱爲接口,這個每每在不少高級語言中不能多重繼承的狀況下使用協議是一個比較好的多態方式。

static

用來修飾變量或函數爲靜態

struct

用來修飾結構體。

subscript

下標修飾,可使類(class),結構體(struct),枚舉(enum) 使用下標訪問。
[cpp]  view plain  copy
  在CODE上查看代碼片 派生到個人代碼片
  1. class Garage  
  2. {  
  3.     var products : String[] = Array()  
  4.       
  5.     subscript(index:Int) -> String  
  6.     {  
  7.         get  
  8.         {  
  9.             return products[index]  
  10.         }  
  11.           
  12.         set  
  13.         {  
  14.             if index < products.count  //&& !products.isEmpty  
  15.             {  
  16.                 products[index] = newValue  
  17.             }  
  18.             else  
  19.             {  
  20.                 products.append(newValue)  
  21.             }  
  22.               
  23.         }  
  24.     }  
  25. }  

測試:
[cpp]  view plain  copy
  在CODE上查看代碼片 派生到個人代碼片
  1. func testSubscript()  
  2. {  
  3.     var garage = Garage()  
  4.     garage[0] = "A"  
  5.     garage[1] = "B"  
  6.     garage[2] = "C"  
  7.     garage[3] = "D"  
  8.     garage[2] = "CC"  
  9.       
  10.     println("index 1 = \(garage[0]) ,index 2 = \(garage[1]),index 3 = \(garage[2]) ,index 4 = \(garage[3])")  
  11. }  

輸出
[cpp]  view plain  copy
  在CODE上查看代碼片 派生到個人代碼片
  1. index 1 = A ,index 2 = B,index 3 = CC ,index 4 = D  

typealias

類型別名,就像typedef同樣。借typedef  unsigned long int    UInt64 

一樣在swift中也可能自定義類型。


break

跳出循環,一般用於for,while,do-while,switch 

case

case相信你們並不陌生,常在switch中使用,但現在在swift中多了一個地方使用哪就是枚舉類型。

continue

跳過本次循環,繼續日後執行。

default

缺省聲明。常見在switch中。

do, else,if, for, return, switch, while

這幾個就不用多說了,越說越混。

in

範圍或集合操做

[cpp]  view plain  copy
  在CODE上查看代碼片 派生到個人代碼片
  1. let str = "123456"  
  2. for c in str  
  3. {  
  4.      println(c)  
  5. }  

fallthrough

因爲swift中的switch語句中能夠省去了break的寫法,但在其它語言中省去break裏,會繼續日後一個case跑,直到碰到break或default才完成。在這裏fallthrough就如同其它語言中忘記寫break同樣的功效。

[cpp]  view plain  copy
  在CODE上查看代碼片 派生到個人代碼片
  1. let integerToDescribe = 1  
  2. var description = "The number \(integerToDescribe) is"  
  3. switch integerToDescribe {  
  4. case 1, 3, 5, 7, 11, 13, 17, 19:  
  5.     description += " a prime number, and also";  
  6.     fallthrough  
  7. case 5:  
  8.     description += " an integer"  
  9. default :  
  10.     description += " finished"  
  11. }  
  12.   
  13. println(description)  

輸出:
[cpp]  view plain  copy
  在CODE上查看代碼片 派生到個人代碼片
  1. The number 1 is a prime number, and also an integer  

where

swift中引入了where 來進行條件判斷。

[cpp]  view plain  copy
  在CODE上查看代碼片 派生到個人代碼片
  1. let yetAnotherPoint = (1, -1)  
  2. switch yetAnotherPoint {  
  3. case let (x, y) where x == y:  
  4. println("(\(x), \(y)) is on the line x == y")  
  5. case let (x, y) where x == -y:  
  6. println("(\(x), \(y)) is on the line x == -y")  
  7. case let (x, y):  
  8. println("(\(x), \(y)) is just some arbitrary point")  
  9. }  

當switch的條件知足where 後面的條件時,才執行語句。

is

as

is 經常使用於對某變量類型的判斷,就像OC中 isKindClass ,as 就有點像強制類型轉換的意思了。

[cpp]  view plain  copy
  在CODE上查看代碼片 派生到個人代碼片
  1. for view : AnyObject in self.view.subviews  
  2. {  
  3.     if view is UIButton  
  4.     {  
  5.         let btn = view as UIButton;  
  6.         println(btn)  
  7.     }  
  8. }  

OC的寫法:
[cpp]  view plain  copy
  在CODE上查看代碼片 派生到個人代碼片
  1. for (UIView *view  in self.view.subviews)  
  2. {  
  3.       if ([view isKindOfClass:[UIButton class]])         //is 操做  
  4.      {  
  5.              UIButton *btn =(UIButton *)view             //as 操做  
  6.       }  
  7. }  

super

基類的關鍵語,一般稱父類

__COLUMN__, __FILE__, __FUNCTION__, __LINE__

是否是有點像宏定義啊。

[cpp]  view plain  copy
  在CODE上查看代碼片 派生到個人代碼片
  1. println(__COLUMN__ ,__FILE__, __FUNCTION__, __LINE__)  

輸出:
[cpp]  view plain  copy
  在CODE上查看代碼片 派生到個人代碼片
  1. (17, /Users/apple/Desktop/swiftDemo/swiftDemo/ViewController.swift, viewDidLoad(), 62)  

set,get

經常使用於類屬性的setter getter操做。

willSet,didSet

在swift中對set操做進行了擴展,willset 在set新值成功前發生,didset在設置新值成功後發生。

inout

對函數參數做爲輸出參數進行修飾。

[cpp]  view plain  copy
  在CODE上查看代碼片 派生到個人代碼片
  1. func swapTwoInts(inout a: Int, inout b: Int) {  
  2.     let temporaryA = a  
  3.     a = b  
  4.     b = temporaryA  
  5. }  

mutating

具體不是很理解,好像是專爲結構體使用而設置的變體聲明
[cpp]  view plain  copy
  在CODE上查看代碼片 派生到個人代碼片
  1. protocol ExampleProtocol {  
  2.     var simpleDescription: String { get }  
  3.     mutating func adjust()  
  4. }  
  5.   
  6. class SimpleClass: ExampleProtocol {  
  7.     var simpleDescription: String = "A very simple class."  
  8.     func adjust() {  
  9.         simpleDescription += "  Now 100% adjusted."  
  10.     }  
  11. }  
  12.   
  13.   
  14. struct SimpleStructure: ExampleProtocol {  
  15.     var simpleDescription: String = "A simple structure"  
  16.     mutating func adjust() {                //若是去除mutating 報Could not find an overload for '+=' that accepts the supplied arguments  
  17.         simpleDescription += " (adjusted)"  
  18.     }  
  19. }  
測試
[cpp]  view plain  copy
  在CODE上查看代碼片 派生到個人代碼片
  1. func testMutating()  
  2. {  
  3.     var a = SimpleClass()  
  4.     a.adjust()  
  5.     let aDescription = a.simpleDescription  
  6.     println(aDescription)  
  7.       
  8.     var b = SimpleStructure()  
  9.     b.adjust()  
  10.     let bDescription = b.simpleDescription  
  11.     println(bDescription)  
  12. }  
override
父子類之間的函數重寫,即覆蓋。

unowned, unowned(safe), unowned(unsafe)

無宿主引用。

[unowned self] 或[unowned(safe) self] 或[unowned(unsafe) self]


weak

弱引用,使得對象不會被持續佔有

相關文章
相關標籤/搜索