Wiki: Swift

Substrings

Prefix && Suffix (Thank primo)

let str = "Hello! World"
str.prefix(6) // "Hello!"
str.suffix(5) // "World"

Splitting

The split function can take a closure that tells it what characters to split on. Split on spaces using

i.split{$0==" "}

Typecasting

String

String(i)

can be

"\(i)"

Array

Array(i)

can be

[]+i

Looping

for i in 0...100{print(i)}
(0...100).map{print($0)}

Conditional printing

Usually the same length, but depending on specific situation one can be shorter than the other

if a>0{print(a)}
a>0 ?print(a):()