Swift 學(xué)習(xí) day03 2016.1.20

字典 集合 函數(shù)

//一直記不住什么是可選綁定

1.創(chuàng)建一個(gè)字典

var dictionary : Dictionary<String, Int> = Dictionary<String, Int>()//最標(biāo)準(zhǔn)的聲明一個(gè)字典
var dictionary2 = Dictionary<String, Int>()

var dictionary6 : [String:Int] = [String:Int]()//個(gè)人認(rèn)為這種事最簡(jiǎn)單地聲明
var dictionary7 : [String:Int] = ["age":37, "age2":18]
var dictionary8 = ["age":37, "age2":18] //個(gè)人認(rèn)為這種是最簡(jiǎn)單地寫出一個(gè)字典

字典增

如果key不存在 是 添加 如果存在是修改原內(nèi)容

var airports : [String : String] = ["PEK":"北京首都機(jī)場(chǎng)", "CAN":"廣州白云機(jī)場(chǎng)", "SHA":"上海虹橋機(jī)場(chǎng)"]
/******* 增 **********/
//如果key不存在 是 添加  如果存在是修改原內(nèi)容
airports["SZX"] = "深圳寶安機(jī)場(chǎng)" //簡(jiǎn)單寫法
airports.updateValue("上海浦東機(jī)場(chǎng)", forKey: "PVG")//標(biāo)準(zhǔn)寫法
airports["TRA"] = "達(dá)內(nèi)機(jī)場(chǎng)"
print(airports)

字典刪

//可選綁定
if let airport = airports.removeValueForKey("TRA") {
    print("\(airport) 刪除成功")
}else {
    print("沒(méi)有對(duì)應(yīng)的機(jī)場(chǎng)")
}

字典改

airports["SZX"] = "高級(jí)深圳寶安機(jī)場(chǎng)"
print(airports)
//返回修改前的內(nèi)容
if let oldValue = airports.updateValue("深圳寶安機(jī)場(chǎng)", forKey: "SZX") {
    print("刪除成功 刪除前是 \(oldValue)")
}else {
    print("沒(méi)有找到要?jiǎng)h除的機(jī)場(chǎng), 添加新的機(jī)場(chǎng)")
}

字典查

if let airportName = airports["SZX"] {
    print("找到對(duì)應(yīng)機(jī)場(chǎng) \(airportName)")
}else {
    print("沒(méi)有找到對(duì)應(yīng)機(jī)場(chǎng)")
}

字典遍歷


for (airportCode,airportname) in airports{
    print("字典 key \(airportCode) 對(duì)應(yīng)的 value \(airportname)")
}


//可以通過(guò)字典的所有key 來(lái)創(chuàng)建一個(gè)數(shù)組
let airportCodes : [String] = [String](airports.keys)
//可以通過(guò)字典的所有value 來(lái)創(chuàng)建一個(gè)數(shù)組
let airportNames : [String] = [String](airports.values)

2.集合

其實(shí)集合字典數(shù)組 根 OC 里面的差不多,就是寫法不一樣了而已
Set 和數(shù)組的區(qū)別:沒(méi)有重復(fù)的數(shù)據(jù) ,無(wú)序


var favoriteGenres : Set<String> = ["Rock", "Classical", "Jazz", "Hip hop"]

var favoriteGenres : Set<String> = ["Rock", "Classical", "Jazz", "Hip hop"]

//查看 集合中 是否包含 指定元素
favoriteGenres.contains("Jazz")

//給集合排序 
for genre in favoriteGenres.sort() {
    print("排好順序的元素 是  \(genre)")
}


let oddDigits : Set = [1, 3, 5, 7, 9]
let evenDigits : Set = [0, 2, 4, 6, 8, 1, 3]
//并集
var new = oddDigits.union(evenDigits).sort()
oddDigits
new
//交集
oddDigits.intersect(evenDigits)
//把兩個(gè)集合 都存在元素 去除掉 返回一個(gè)新的繼承
oddDigits.subtract(evenDigits)
//把兩個(gè)集合中 相同的元素去除掉 用兩個(gè)集合剩下的元素 來(lái)創(chuàng)建一個(gè)新的集合并返回
oddDigits.exclusiveOr(evenDigits).sort()

3.function

/*
如何聲明定義一個(gè)函數(shù)
func 函數(shù)名稱(參數(shù)列表) ->返回值類型 {
函數(shù)體
}
*/

func sayHello(personName : String) ->String {
    let greeting = "Hello " + personName + " !"
    return greeting
}





//有多個(gè)返回值  結(jié)合元組
let string = "我愛(ài)你, 我想你, 我喜歡你, 我恨你, 我嫁你"
func count(str : String) ->(me:Int,you:Int,other:Int) {
    var m = 0, y = 0, o = 0
    for char in str.characters {
        switch char {
        case "我":
            m++
        case "你":
            y++
        default:
            o++
        }
    }
    return (m, y, o)
}

let result /*: (me:Int, you:Int, others:Int)*/ = count(string)
result.me
result.you
result.other
result.0
result.1
result.2

函數(shù)的形參 (外部名,內(nèi)部名)


//函數(shù)的行參
func fa(localName/*參數(shù)的內(nèi)部名*/ : Int) {
    //內(nèi)部名 是 函數(shù)里面使用的
    print("\(localName)")
}
fa(100)

func fa(externalName/*參數(shù)的外部名*/ localName/*參數(shù)的內(nèi)部名*/ : Int) {
    //內(nèi)部名 是 函數(shù)里面使用的
    print("\(localName)")
}
//參數(shù)的 外部名 在調(diào)用參數(shù)時(shí)使用 (函數(shù)外面使用)
fa(externalName : 100)


//函數(shù)的第二個(gè)及以后的參數(shù)名稱,默認(rèn)即是外部名,也是內(nèi)部名
func show(name : String, age : Int) {
    //name 是內(nèi)部名
    //age 既是內(nèi)部名 也是 外部名
    print("\(name) 的年齡是 \(age)")
}
//age 是外部名
show("張三", age: 30)

//如果不希望使用默認(rèn)的外部名 可以自己起外部名
func pointWithX(x : Int, AndY y : Int) {
    //x 和 y 都是內(nèi)部名   AndY是外部名
    print("Point( \(x), \(y) )")
}


//使用 _  取消外部名
func point(x : Int,_ y : Int) {
    print("Point( \(x), \(y) )")
}


//參數(shù)的默認(rèn)值  如果有默認(rèn)值得參數(shù),我們傳入任何內(nèi)容就使用默認(rèn)值,如果傳了內(nèi)容就使用我們傳入的
func printArray(array : [Int], s : String = ",", flag : Bool = false) {
    if flag {
        print("[")
    }
    for var i = 0; i < array.count - 1; i++ {
        print("\(array[i])\(s)")
    }
    print(array[array.count-1])
    if flag {
        print("]")
    }
}


//可變長(zhǎng)參數(shù) 使用...
func arithmeticMean(numbers : Double...) ->Double{
    var total = 0.0
    for number in numbers {
        print(number)
        total += number
    }
    return total / Double(numbers.count)
}

//函數(shù)的參數(shù)默認(rèn)情況下 是 常量, 如果需要在函數(shù)中修改參數(shù)必須聲明成變量


func fa(var x : Int) {
    x = 105
    print(x)
}
fa(100)

//inout 輸入輸出參數(shù) 相當(dāng)C中傳地址

func mySwap2(inout x : Int, inout y : Int) {
//    let t = x
//    x = y
//    y = t
    x = x ^ y //異或
//        0011
//        0101
//    x =----------
//        0110
    y = x ^ y
//        0110
//        0101
//    y =----------
//        0011   3
    x = x ^ y
//        0110
//        0011
//    x =----------
//        0101   5
}

//輸入輸出參數(shù) 傳的時(shí)候必須 加 & 符號(hào)
mySwap2(&a, y : &b)

//結(jié)構(gòu) nil 返回的返回值一定是可選值

//結(jié)構(gòu) nil 返回的返回值一定是可選值
func minMax(array : [Int]) ->(max : Int, min : Int)? {
    if array.isEmpty {
        return nil
    }
    return (array[0], array[0])
}

/****** 函數(shù)類型 *********************/


func sayHello() {  //()->()
    print("hello")
}
//(Int,Int)->Int
func addTwoInts(a:Int, b:Int) ->Int{
    return a + b
}
func subTwoInts(a:Int, b:Int) ->Int{
    return a - b
}




//閉包函數(shù)
mc.getResult({ (a, b) -> Int in
    return a % b + 200
    }
)

var _as : [Int] = [1, 2, 3, 4]
_as[0]
//函數(shù)數(shù)組
var funcs : [(Int,Int)->Int] = [addTwoInts, subTwoInts, mulTwoInts]
funcs[0](100, 200)
funcs[1](100, 200)
funcs[2](100, 200)

//函數(shù)做為返回值
func test() ->(Int,Int)->Int {
    return addTwoInts
}
test()(100, 200)

自己覺(jué)得不難,,消化的還行,跟 OC 差不多
明天去面試 ,不能上課了 good luck!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容