Skip to content

Instantly share code, notes, and snippets.

@TarasShu
Last active June 16, 2020 10:35
Show Gist options
  • Save TarasShu/86a28d8170c9bbcdf9e5fe747e7040ab to your computer and use it in GitHub Desktop.
Save TarasShu/86a28d8170c9bbcdf9e5fe747e7040ab to your computer and use it in GitHub Desktop.
func swapTwoString(a: inout String, b: inout String) {
let temporaryB = a
a = b
b = temporaryB
}
var someString = "I AM FIRST"
var anotherStirng = "I AM SECONDE"
print("first string is \(someString) and another Stirng is \(anotherStirng) ")
swapTwoString(a: &someString, b: &anotherStirng)
print("now first string is \(someString) and another Stirng is \(anotherStirng)")
var someString = 1
var anotherStirng = 2
print("first string is \(someString) and another Stirng is \(anotherStirng) ")
swapTwoString(a: &someString, b: &anotherStirng)
print("now first string is \(someString) and another Stirng is \(anotherStirng)")
// Cannot convert value of type 'Int' to expected argument type 'String'
//////////////////////////////
//generics
func swapTwoAnyValues<Values>(a: inout Values, b: inout Values) {
let temporaryB = a
a = b
b = temporaryB
}
var anyValues = 1
var anotherAnyValues = 2
print("first string is \(anyValues) and another Stirng is \(anotherAnyValues) ")
swapTwoAnyValues(a: &anyValues, b: &anotherAnyValues)
print("now first string is \(anyValues) and another Stirng is \(anotherAnyValues)")
var anyValues1 = "I AM FIRST"
var anotherAnyValues1 = "I AM SECONDE"
print("first string is \(anyValues1) and another Stirng is \(anotherAnyValues1) ")
swapTwoAnyValues(a: &anyValues1, b: &anotherAnyValues1)
print("now first string is \(anyValues1) and another Stirng is \(anotherAnyValues1)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment