「Swift」タプル(tuple)にオプショナル型を扱えるサンプル

書式
let タプル名=(Intオプショナル型名,Stringオプショナル型名)

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
//オプショナル型Int?
let age: Int? = 21
//オプショナル型String?
let str: String? = "study"
//オプショナル型Double?
let price:Double? = 23.45
//タプルにオプショナル型を入れる
let tp = (age, str, price)
switch tp {
case (21?, "study"?,23.45?):
print("オプショナル型Int String Double条件値が一致")
default:
print("条件値と一致しません")
}
//オプショナル型Int? let age: Int? = 21 //オプショナル型String? let str: String? = "study" //オプショナル型Double? let price:Double? = 23.45 //タプルにオプショナル型を入れる let tp = (age, str, price) switch tp { case (21?, "study"?,23.45?): print("オプショナル型Int String Double条件値が一致") default: print("条件値と一致しません") }
//オプショナル型Int?
let age: Int? = 21
//オプショナル型String?
let str: String? = "study"
//オプショナル型Double?
let price:Double? = 23.45

//タプルにオプショナル型を入れる
let tp = (age, str, price)

switch tp {
case (21?, "study"?,23.45?):
    print("オプショナル型Int String Double条件値が一致")
default:
    print("条件値と一致しません")
}

結果
オプショナル型Int String Double条件値が一致

Swift

Posted by arkgame