Created
December 21, 2016 07:49
-
-
Save josephzhang23/9c120cbda796e25f99bb5ba83fc4667d to your computer and use it in GitHub Desktop.
Swift 泛型
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
上面定义的 swapTwoValues(::) 函数是受 swap(::) 函数启发而实现的。后者存在于 Swift 标准库,你可以在你的应用程序中使用它。如果你在代码中需要类似 swapTwoValues(::) 函数的功能,你可以使用已存在的 swap(::) 函数。