6. Declaration of a constant with type inference and without semicolon
let name = "Maxim"
7. Declaration of constant immutable array
let names : Array<String> = ["Maxim", "Anton"]
let names : [String] = ["Maxim", "Anton"]
let names = ["Maxim", "Anton"]
names.append("Fox") // Compile error
8. Declaration of mutable array (as variable)
var names = ["Maxim", "Anton"]
names.append("Fox") // ["Maxim", "Anton", "Fox"]
9. Declaration of Optional value
var name : Optional<String> = "Maxim"
name!.isEmpty == false // ???
var name : String? = nil
name?.isEmpty == false // ???
10. Declaration of unwraped value if applicable
var name : String? = nil
if let _name = name {
print("(_name)")
} else {
print("No name!")
}
11. Declaration of value which is only optional in the beginning
var name : ImplicitlyUnwrappedOptional<String> = "Maxim"
name.isEmpty
var name : String! = nil
name.isEmpty // Runtime Exception
12. Decomposition of touples
var a = 0, b = 0
let touple = (20, 30)
a = touple.0
b = touple.1
// Or just like this:
(a, b) = touple
13. Convert from literal
let url : NSURL = "http://nshipster.com/"
"http://nshipster.com/".host
14. extension NSURL: StringLiteralConvertible {
public class func convertFromExtendedGraphemeClusterLiteral(value: String) -> Self {
return self(string: value)
}
public class func convertFromStringLiteral(value: String) -> Self {
return self(string: value)
}
}
18. Function without parameter label
func f(_ v : Int) -> Int { return v + 1 }
func f(v : Int) -> Int { return v + 1 }
let v1 = f(10) // v1 = 11
19. Functoin with parameter label equal to parameter name
func f(value value : Int) -> Int { return value + 1 }
func f(#value : Int) -> Int { return value + 1 }
let v1 = f(value : 10) // v1 = 11
21. Copy parameter in to variable
func f(var v : [String]) -> [String] {
v.append("Anton")
return v
}
let a = ["Maxim"]
let a2 = f(a) // a = ["Maxim"] , a2 = ["Maxim", "Anton"]
25. Returning a closure (idea behind currying)
func f(a : Int) -> (Int -> Int) {
let f1 = {
(b: Int) -> Int in
return a + b
}
return f1
}
let v1 = f(1)(2)
26. Returning a closure (idea behind currying)
func f(a : Int) -> (Int -> Int) {
return {
(b: Int) -> Int in
return a + b
}
}
let v1 = f(1)(2)
27. Remove Closure types
func f(a : Int) -> (Int -> Int) {
return {
b in
return a + b
}
}
let v1 = f(1)(2)
28. Remove return keyword
func f(a : Int) -> (Int -> Int) {
return {
b in a + b
}
}
let v1 = f(1)(2)
29. Remove parameter name
func f(a : Int) -> (Int -> Int) {
return {
a + $0
}
}
let v1 = f(1)(2)
30. Call a function with value
func f(sum : Int) {
print("sum is (sum)")
}
f(1 + 2) // f(3)
31. Call function with closure enables lazy evaluation
func f(sum : () -> Int) {
print("sum is (sum())")
}
f({1 + 2}) // lazy evaluation
32. Call function with auto closure enables lazy evaluation implicitly
func f(sum : @autoclosure () -> Int) {
print("sum is (sum())")
}
f(1 + 2) // still lazy evaluation
33. If last operator is a function you can take it out of parenthesis
func f(a : Int, b: Int, operation : (Int,Int) -> Int) {
print("sum is ( operation(a, b) )")
}
f(1,2) {
$0 + $1
}
34. Or keep it
func f(a : Int, b: Int, operation : (Int,Int) -> Int) {
print("sum is ( operation(a, b) )")
}
f(1, 2, +)
35. Operators are functions
1 + 2
infix operator + {
associativity left
precedence 140
}
func +(lhs: Int, rhs: Int) -> Int
36. Execution is based on precedence
1 + 2 * 4
infix operator + {
associativity left
precedence 140
}
infix operator * {
associativity left
precedence 150
}
38. Touple with named values can be used as structs
typealias MyPoint = (x:Int, y:Int)
let point : MyPoint = (x:25, y:30)
point.x // 25
point.y // 30
39. Struct with implicit intializer
struct MyPoint {
let x : Int
let y : Int
}
let point = MyPoint(x:25, y:30)
point.x // 25
point.y // 30
40. Methods are curried functions
struct MyName {
let name : String
init(_ n : String) {
name = n
}
func desc()->String{
return "My name is: (name)"
}
}
let myName = MyName("Maxim")
myName.desc()
MyName.desc(myName)()