原文地址: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
用來定義一個類,相信你們並不陌生。
若是定義一個汽車類
- class Car
- {
- init()
- {
- //to do init something.
- }
- }
init
相對於類的構造方法的修飾。
deinit
相對於類的釋構方法的修飾。
對於類的構造和釋構在swift 中須要使用關鍵詞來修飾,而不少高級語言並不須要特別的指定,便C++ 只須要類名與構造函數名相同就能夠,不須要額外的關鍵詞。
enum
枚舉類型的聲明,這個與不少語方都相通。
extension
擴展,有點像oc中的categories 。
Swift 中的能夠擴展如下幾個:
添加計算型屬性和計算靜態屬性
定義實例方法和類型方法
提供新的構造器
定義下標
定義和使用新的嵌套類型
使一個已有類型符合某個接口
以下面擴展字符串:
- extension String{
- struct _Dummy {
- var idxVal: Int
- var _padding: Int
- var _padding2: Int
- var _padding3: Int
- }
- //過慮出數字
- func fitlerCharater() -> String
- {
- var numberstr : String = ""
- for character in self
- {
- let s :String = String(character)
-
- //println(s.toInt())
- if let hs = s.toInt()
- {
- numberstr += character
- }
- }
- return numberstr
- }
-
- //擴展使用下標訪問
- subscript (i: Int) -> Character {
- var dummy: _Dummy = reinterpretCast(i >= 0 ? self.startIndex : self.endIndex)
- dummy.idxVal += i
- let idx: String.Index = reinterpretCast(dummy)
- return self[idx]
- }
-
- //擴展使用Range訪問
- subscript (subRange: Range<Int>) -> String {
- var start: _Dummy = reinterpretCast(self.startIndex)
- var end = start
- start.idxVal = subRange._startIndex
- end.idxVal = subRange._endIndex
- let startIndex: String.Index = reinterpretCast(start)
- let endIndex: String.Index = reinterpretCast(end)
- return self[startIndex..endIndex]
- }
- }
測試:
- func testExtension()
- {
- var str : String = "1234ab5國6cd7中8i90"
- println(str.fitlerCharater())
-
- let china: String = "china operating system public to 世界"
- println("使用下標索引訪問第13個字符 \(china[13])")
- println("使用負號下標即變爲從右往左訪問字符 \(china[-1])")
- println("使用負號下標即變爲從右往左訪問字符 \(china[-2])")
- println("使用下標Range來訪問範圍 \(china[2...6])")
- dump(china[1..5], name: "china[1:4]") //使用dump輸出
- dump(china[10...13], name: "china[10:13]")
- }
輸出:
- 1234567890
- 使用下標索引訪問第13個字符 n
- 使用負號下標即變爲從右往左訪問字符 界
- 使用負號下標即變爲從右往左訪問字符 世
- 使用下標Range來訪問範圍 ina o
- - china[1:4]: hina
- - china[10:13]: atin
func
用來修飾函數的關鍵詞。
import
導入頭文件,相信你們都不陌生,但在swift 中好像被用來導入包,如import UIKit。 由於swift中沒有了頭文件的概念。
let
用來修改某一常量的關鍵詞。像const 限定差很少
var
用來聲明變量。
protocol
協議,也有稱爲接口,這個每每在不少高級語言中不能多重繼承的狀況下使用協議是一個比較好的多態方式。
static
用來修飾變量或函數爲靜態
struct
用來修飾結構體。
subscript
下標修飾,可使類(class),結構體(struct),枚舉(enum) 使用下標訪問。
- class Garage
- {
- var products : String[] = Array()
-
- subscript(index:Int) -> String
- {
- get
- {
- return products[index]
- }
-
- set
- {
- if index < products.count //&& !products.isEmpty
- {
- products[index] = newValue
- }
- else
- {
- products.append(newValue)
- }
-
- }
- }
- }
測試:
- func testSubscript()
- {
- var garage = Garage()
- garage[0] = "A"
- garage[1] = "B"
- garage[2] = "C"
- garage[3] = "D"
- garage[2] = "CC"
-
- println("index 1 = \(garage[0]) ,index 2 = \(garage[1]),index 3 = \(garage[2]) ,index 4 = \(garage[3])")
- }
輸出
- 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
範圍或集合操做
- let str = "123456"
- for c in str
- {
- println(c)
- }
fallthrough
因爲swift中的switch語句中能夠省去了break的寫法,但在其它語言中省去break裏,會繼續日後一個case跑,直到碰到break或default才完成。在這裏fallthrough就如同其它語言中忘記寫break同樣的功效。
- let integerToDescribe = 1
- var description = "The number \(integerToDescribe) is"
- switch integerToDescribe {
- case 1, 3, 5, 7, 11, 13, 17, 19:
- description += " a prime number, and also";
- fallthrough
- case 5:
- description += " an integer"
- default :
- description += " finished"
- }
-
- println(description)
輸出:
- The number 1 is a prime number, and also an integer
where
swift中引入了where 來進行條件判斷。
- let yetAnotherPoint = (1, -1)
- switch yetAnotherPoint {
- case let (x, y) where x == y:
- println("(\(x), \(y)) is on the line x == y")
- case let (x, y) where x == -y:
- println("(\(x), \(y)) is on the line x == -y")
- case let (x, y):
- println("(\(x), \(y)) is just some arbitrary point")
- }
當switch的條件知足where 後面的條件時,才執行語句。
is
as
is 經常使用於對某變量類型的判斷,就像OC中 isKindClass ,as 就有點像強制類型轉換的意思了。
- for view : AnyObject in self.view.subviews
- {
- if view is UIButton
- {
- let btn = view as UIButton;
- println(btn)
- }
- }
OC的寫法:
- for (UIView *view in self.view.subviews)
- {
- if ([view isKindOfClass:[UIButton class]]) //is 操做
- {
- UIButton *btn =(UIButton *)view //as 操做
- }
- }
super
基類的關鍵語,一般稱父類
__COLUMN__, __FILE__, __FUNCTION__, __LINE__
是否是有點像宏定義啊。
- println(__COLUMN__ ,__FILE__, __FUNCTION__, __LINE__)
輸出:
- (17, /Users/apple/Desktop/swiftDemo/swiftDemo/ViewController.swift, viewDidLoad(), 62)
set,get
經常使用於類屬性的setter getter操做。
willSet,didSet
在swift中對set操做進行了擴展,willset 在set新值成功前發生,didset在設置新值成功後發生。
inout
對函數參數做爲輸出參數進行修飾。
- func swapTwoInts(inout a: Int, inout b: Int) {
- let temporaryA = a
- a = b
- b = temporaryA
- }
mutating
具體不是很理解,好像是專爲結構體使用而設置的變體聲明
- protocol ExampleProtocol {
- var simpleDescription: String { get }
- mutating func adjust()
- }
-
- class SimpleClass: ExampleProtocol {
- var simpleDescription: String = "A very simple class."
- func adjust() {
- simpleDescription += " Now 100% adjusted."
- }
- }
-
-
- struct SimpleStructure: ExampleProtocol {
- var simpleDescription: String = "A simple structure"
- mutating func adjust() { //若是去除mutating 報Could not find an overload for '+=' that accepts the supplied arguments
- simpleDescription += " (adjusted)"
- }
- }
測試
- func testMutating()
- {
- var a = SimpleClass()
- a.adjust()
- let aDescription = a.simpleDescription
- println(aDescription)
-
- var b = SimpleStructure()
- b.adjust()
- let bDescription = b.simpleDescription
- println(bDescription)
- }
override
父子類之間的函數重寫,即覆蓋。
unowned, unowned(safe), unowned(unsafe)
無宿主引用。
[unowned self] 或[unowned(safe) self] 或[unowned(unsafe) self]
weak
弱引用,使得對象不會被持續佔有