Swift的筆記和參考

2022-07-13 11:54:10 字數 4677 閱讀 4168

好久沒來了,趁著新語言swift發布,繼續鑽研中!

create class 建立類 (過載效果)

//

create class 建立類

class

myclass

//method 成員方法

func doit()

func doit() ->int

func doit(a:int) ->int

func doit(a:int, b:int) ->int

func doit() ->string

func doit(a:string) ->string

func doit(a:string, b:string) ->string }//

create / using an instance 建立 / 使用 乙個例項

var a =myclass()

a.doit(

"wang

", b: "

zhipeng

")

enums 列舉

//

enums 列舉

enum

comcsofttype: int

var mytype = comcsofttype.developmentengineer

declaring variables 變數的宣告 (可選變數)

//

declaring variables 變數的宣告

var mutabledouble:double = 1.0

mutabledouble = 2.0

let constantdouble:double = 1.0

//constantdouble = 2.0 error 錯誤

var autodouble = 1.0

//optional value 可選變數 (新機制)

var optiondouble:double? //

optiondouble = 1.0

//這時候開始 optiondouble 才會開始分配記憶體

if let definedouble =optiondouble

else

control flow 控制流

//

control flow 控制流

var condition = true

ifcondition

else

var val = "

four

"switch

val

//omits upper value, use ... to include 省略了上限值,使用 ... 包括

for i in

0..3

for var j = 0; j < 3; ++j

//while

var n = 2

while n < 100

println(n)

var m = 2do

while m < 100

println(m)

string quick examples 字串的例子

//

string quick examples 字串的例子

var firstname = "

zhipeng

"var lastname = "

wang

"var hellostring = "

hello, \(lastname) \(firstname)

"var tipstring = "

2499

"var tipint =tipstring.toint()

extension double

}tipstring = "

24.99

"var tipdouble = double(string:tipstring)

array quick examples 陣列的例子

//

array quick examples 陣列的例子

var person1 = "

one"

var person2 = "

two"

var array:string =[person1, person2]

array += "

three

"for person in

array

var persontwo = array[1

]println(

"persontwo: \(persontwo)

")

dictionary quick examples 字典的例子 (泛型效果)

//

dictionary quick examples 字典的例子

var dic:dictionary= ["

one": "1"

,

"two

": "2"

,

"three

": "3"

]dic[

"three

"] = "4"

//update three

dic["

one"] = nil //

delete one

for(key, value) in

dic

function 函式

//

function 函式

func getpirces() ->(double, double, double)

//取出元組

var (one, two, three) =getpirces()

println(

"one = \(one), two = \(two), three = \(three)")

func getsum(numbers:int...) ->int

return

sum}

getsum()

getsum(

1, 3, 5, 7, 9)

函式巢狀

//

函式巢狀

func nestedfunction() ->int

//add()

return

x}

函式傳遞

//

函式傳遞

func makeincrementer() -> (int ->int)

return

addone

}var increment =makeincrementer()

increment(1)

func makeincrementer2() -> ((string, string) ->string)

return

and}

var increment2 =makeincrementer2()

increment2(

"wang

", "

zhieng")

func lessthanone(number:int) ->bool

func makeincrementer3(list:int, condition:int -> bool) ->bool

}return

false

}makeincrementer3([

1,2,3,4,5], lessthanone)

extends 繼承 (多型效果)

//

extends 繼承 (多型效果)

println("

********************=== extends 繼承")

class

people

//sleep 睡

func sleep()

//work 工作

func work()

//said 說

func said()

}class

chinese:people

}class

americans:people

}class

alien }//

不支援多繼承

//class futuregenerations:chinese, americans //}

//多型 必要條件:1.繼承 2.重寫 3.父類引用指向子類物件

var people =people()

people.said()

people =chinese()

people.said()

people =americans()

people.said()

//people = alien() error 'alien' is not convertible to 'people'

var chinese =chinese()

chinese.said()

var americans =americans()

americans.said()

Swift備忘單和快速參考(持續更新 )

本文是蘋果swift程式語言的備忘單和參考之南,以後會涵蓋swift的所有關鍵特性,包括strings arrays dictionaries以及flow control。swift是蘋果在wwdc 2014上發布的適用於ios和os x平台應用的開發。持續更新的內容,歡迎你來貼自己的備忘單 var...

swift學習筆記

1 值永遠不會被隱式轉換為其他型別。如果你需要把乙個值轉換成其他型別,請顯式轉換。let label the width is let width 94 let widthlabel label string width could not find an overload for that acc...

Swift學習筆記

常量 let product constant iphone6 變數 var product var ipad 不需要宣告變數型別,會根據右側的值推導左側變數的型別 可以多個變數定義在一起 var x1 30,x2 abc 可以精確指定變數的型別 var x1 int 27 指定x1為int型 pr...