Skip to content

Instantly share code, notes, and snippets.

@josephzhang23
Created December 21, 2016 07:49
Show Gist options
  • Select an option

  • Save josephzhang23/9c120cbda796e25f99bb5ba83fc4667d to your computer and use it in GitHub Desktop.

Select an option

Save josephzhang23/9c120cbda796e25f99bb5ba83fc4667d to your computer and use it in GitHub Desktop.
Swift 泛型
func swapTwoValues<Joseph>(_ a: inout Joseph, _ b: inout Joseph) {
let temporaryA = a
a = b
b = temporaryA
}
var someInt = 3
var anotherInt = 107
swapTwoValues(&someInt, &anotherInt)
print("someInt 现在是\(someInt),anotherInt 现在是 \(anotherInt)")
var someString = "hello"
var anotherString = "world"
swapTwoValues(&someString, &anotherString)
print(someString + anotherString)
@josephzhang23
Copy link
Author

上面定义的 swapTwoValues(::) 函数是受 swap(::) 函数启发而实现的。后者存在于 Swift 标准库,你可以在你的应用程序中使用它。如果你在代码中需要类似 swapTwoValues(::) 函数的功能,你可以使用已存在的 swap(::) 函数。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment