#Swift 3 cheat sheet
- Empty Strings
var anEmptyString = ""
var anotherEmptyString = String()
if anEmptyString.isEmpty {
print("String is empty")
}
- stride functions
let fourToTwo = Array(stride(from: 4, to: 1, by: -1)) // [4, 3, 2]
let fourToOne = Array(stride(from:4, through: 1, by: -1)) // [4, 3, 2, 1]
- Mutable parameters are not favorable in Swift functional programming and are removed from Swift 3.0. Functions can have inout parameters.
func swapTwoInts( a: inout Int, b: inout Int) {
let temporaryA = a
a = b
b = temporaryA
}
- nested functions
func returnTwenty() -> Int {
var y = 10
func add() {
y += 10
}
add()
return y
}
returnTwenty()
-
AnyObject can represent an instance of any class type. Any can represent an instance of any type, including structs, enumerations, and function types.
-
Swift enables us to declare nested types whereby we nest supporting enumerations, classes, and structures within the definition of the type that they support.
-
The static methods cannot be overridden by the subclasses of the object that they reside in. Class methods are like the static methods but they can be overridden by subclasses.