Last active
June 1, 2017 18:41
-
-
Save ChenCodes/b2c9da4e33452e1031805803a25158ed to your computer and use it in GitHub Desktop.
Swap Function that swaps the values of two variables of any type
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
//When it says Any type, this means that we're dealing with generics | |
func swap<G>(item_1: inout G, item_2: inout G) { | |
let temp = item_1 | |
item_1 = item_2 | |
item_2 = temp | |
} | |
var one = "1" | |
var two = "2" | |
print(one, two) //1, 2 | |
swap(&one, &two) | |
print(one, two) //2, 1 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment