「Swift」ジェネリクス(generics)のサンプル

サンプルコード

// 変数交換の変数
func swapToFunc(_ a: inout Int, _ b: inout Int) {
    let tmp = a
    a = b
    b = tmp
}
 
var cftA = 100
var cftB = 200
 
print("before data: \(cftA) and \(cftB)")
swapToFunc(&cftA, &cftB)
print("after data: \(cftA) and \(cftB)")

実行結果
before data: 100 and 200
after data: 200 and 100

Swift

Posted by arkgame