Swift基礎高階

2021-07-11 15:09:10 字數 3396 閱讀 6594

where與模式匹配

guard

string

//(1)

var str = "abc"

if(str == nil)

//string? 為string可選型,他意味著這種型別,可以為nil,還是那句話,可選型和nil不是乙個型別的特殊狀態,他是一種新的型別。

//2var optionalstr:string? = "hello"

optionalstr = "world"

print(optionalstr == nil)

var greet = "hey"

if let name = optionalstr

print(greet)

if(optionalstr != nil)

//switch中使用where語句

let point = (3,3)

switch point

//switch中使用運算子

let age = 19

switch age

//if中使用模式

ifcase

10...19 = age where age > 18

//if中使用模式和where語句

let vector = (4,0)

ifcase (let x,0) = vector where x > 2 && x < 5

//if中使用模式+where+運算子

forcase

let i in

1 ... 100

where i % 3==0

//guard else 防止資料錯誤,也可以認為是需要滿足的一種先決條件

//例如下面money>price,capacity > volume是必須滿足的條件,簡化語法,防止錯誤

func buy(money:int,price:int,capacity:int,volume:int) -> bool

guard capacity > volume else

return

true

}

//字串

var originstr = "this is str"

originstr = "this is new str"

//定義乙個空字串

let emptystr = ""

//判空操作

print(emptystr.isempty)

//合併字串

var rststr = originstr + emptystr

//使用+=,此時rststr必須是變數

rststr += originstr

//字串插值

rststr = "this is new str and insert \(100)"

//轉義字元 \

//遍歷字串

for c in originstr.characters

//character

let cc:character = "!"

//字串長度,基於unicode碼,也就是說不管是3個漢字或者3個字母,它的count都是3

let englishletter = "abc"

let chinseletter = "慕課網"

let emojiletter = "������"

let unicodeletter = "\u\u"

print(englishletter.characters.count)

print(chinseletter.characters.count)

print(emojiletter.characters.count)

print("enlish is \(englishletter) chinse is \(chinseletter) emoji is \(emojiletter) unicode is \(unicodeletter)")

//索引訪問字串,需要使用string.index類來訪問

//[startindex,endindex)

let startindex = originstr.startindex

let endindex = originstr.endindex

print("index is \(startindex) and content is \(originstr[startindex])")

//向後n個

startindex.advancedby(6)

//前面乙個位置

endindex.predecessor()

//後面乙個位置

startindex.successor()

//string的一些api

//range型別

let range = startindex.advancedby(3) ..< endindex

originstr.replacerange(range, with: "!!!")

originstr.insert("z", atindex: originstr.endindex) originstr.removeatindex(originstr.endindex.predecessor()) originstr.removerange(originstr.endindex.advancedby(-2).."upper \(originstr.uppercasestring) low \(originstr.lowercasestring) first up \(originstr.capitalizedstring)")

originstr.containsstring("")

originstr.hassuffix("")

originstr.hasprefix("")

//nsstring

let s2 = nsstring(format: "one third is %.2f",1.0/3.0)

print("轉換 \(s2 as string)")

let nsstr:nsstring = "one third is 0.33"

nsstr.substringfromindex(4)

nsstr.substringfromindex(3)

nsstr.substringwithrange(nsmakerange(4, 5))

let s6 = " --- hello --- "

as nsstring

//去掉空格和-

print(s6.stringbytrimmingcharactersinset(nscharacterset(charactersinstring:" -"))

swift課程 02 swift高階

類與結構體的區別 1 結構體自帶乙個初始化所有屬性的初始化方法,而類帶有乙個不含任何引數的建構函式 2 結構體是值型別,每次拷貝乙份新的內容,修改原來的,不會影響新的 類是引用型別,賦值就像是oc中的指標指向,無論通過哪個變數名修改,都是在操作乙個物件 1.類 類的寫法 注意 宣告類方法 可以在前面...

Swift語法 高階函式

高階函式map,flatmap,reduce,filter可以簡化 量,能用就用。1 map函式,是集合和string類的例項方法,作用是遍歷時修改item map並不會修改例項值,而且新建乙個拷貝。摘自官方文件 let cast vivien marlon kim karl let lowerca...

Swift 高階運算子

高階運算子 向上溢位 向下溢位 除零溢位,0作為被除數時結果是0 模零溢位,對 0求餘結果是0 varwilloverflow uint8 max 等於 255 willoverflow willoverflow 1 等於0 willoverflow willoverflow 1 等於2 varwi...