iOS ● ● ○ Swift 6

Kodebits Day 64: Generic Function

Jul 25 2026
Practice generics with a short swift challenge.

What is the output?

func swap<T>(
  _ a: inout T,
  _ b: inout T
) {
  let temp = a
  a = b
  b = temp
}
var x = 5, y = 9
swap(&x, &y)
print(x)
print(y)


Try it in the online Swift Playground →

[spoiler title="Solution"]

Answer:

9
5

Explanation:

Generic functions use type parameters (like ) to work with any type. The inout keyword allows modifying the original variables. swap exchanges x and y’s values, so x becomes 9 and y becomes 5.

[/spoiler]


Further Reading