swfit #7 Tuple

//: Playground - noun: a place where people can play

import UIKit

let page = (1,"第一頁內容")

page.0
page.1

let (pageIndex,pageContent) = page
pageIndex
pageContent


//定義 tuple 的下標  相似 dict
let  antherPage:(pageIndex:Int, pageText:String) = (10 ,"cccccc text")

//用 下標來訪問
antherPage.pageIndex
antherPage.pageText

//Tuple 做爲函數返回值

func  currentPage() -> (pageInedex:Int , pageText:String) {
    return (1,"內容內容")
}

currentPage().pageText

let  books = [("python 入門",15,509,"計算機"), ("算法導論",4,300,"理論"),
        ("互聯網思惟",6,400 ,"商業")]

// switch tupe _ 表示 忽略
for book in books {
    println("\(book.0) -")
    switch book {
        //定義變量
    case (_,15,let chapterCount,let genre):
        println("這本書是給 python初學者用")
        //區間條件
    case (_,5...7,_,_):
        println("創業書籍")
    default:
        println("其餘書籍")
    }
}

// 利用 Tuple 交換 2個變量值
var name1 = "kai"
var name2 = "super man"

(name1,name2)=(name2,name1)

name1
相關文章
相關標籤/搜索