diff --git a/Swift/SwiftClosures.playground/Pages/Autoclosure.xcplaygroundpage/Contents.swift b/Swift/SwiftClosures.playground/Pages/Autoclosure.xcplaygroundpage/Contents.swift new file mode 100644 index 0000000..5311d77 --- /dev/null +++ b/Swift/SwiftClosures.playground/Pages/Autoclosure.xcplaygroundpage/Contents.swift @@ -0,0 +1,16 @@ +//: [Escaping closure](@previous) + +struct Test { + init() { + print("Test initialized") + } +} + +func testFunc(isEnabled: Bool, closure: @autoclosure () -> Test) { + if isEnabled { + closure() + } +} + +testFunc(isEnabled: true, closure: Test()) +testFunc(isEnabled: false, closure: Test()) diff --git a/Swift/SwiftClosures.playground/Pages/Escaping.xcplaygroundpage/Contents.swift b/Swift/SwiftClosures.playground/Pages/Escaping.xcplaygroundpage/Contents.swift new file mode 100644 index 0000000..51784c5 --- /dev/null +++ b/Swift/SwiftClosures.playground/Pages/Escaping.xcplaygroundpage/Contents.swift @@ -0,0 +1,41 @@ +//: [Closure as function parameter](@previous) + +import Foundation + +struct FunctionStorage { + + typealias ClosureType = (Int, Int) -> Int + + private(set) var funcList: [ClosureType] = [] + + mutating func append(aFunction: @escaping ClosureType) { + funcList.append(aFunction) + } + +/*: ### The functions append() accepts and adds to the array a closure declared outside the function. */ + + mutating func append(functions: @escaping ClosureType...) { + funcList.append(contentsOf: functions) + } + + func apply(to value: Int, + and aValue: Int, + from initial: Int = 0, with action: ClosureType) -> Int { + + return funcList.reduce(initial) { (res, fn) -> Int in + action(res, fn(value, aValue)) + } + } +} + +var storage = FunctionStorage() +storage.append(functions: (*), (+), (-)) +storage.apply(to: 4, and: 2, with: (+)) + + +/*: ### Default closure type is @noescape */ + +//: [Autoclosure](@next) + + + diff --git a/Swift/SwiftClosures.playground/Pages/In function.xcplaygroundpage/Contents.swift b/Swift/SwiftClosures.playground/Pages/In function.xcplaygroundpage/Contents.swift new file mode 100644 index 0000000..92d8a8f --- /dev/null +++ b/Swift/SwiftClosures.playground/Pages/In function.xcplaygroundpage/Contents.swift @@ -0,0 +1,35 @@ +//: [Simple closure](@previous) + +func repeatFunc(num: Int, f: () -> ()) { + for _ in 1...num { + f() + } +} +repeatFunc(num: 5) { + print("Hello world") +} + + + +var square: (Int) -> (Int) = { x in + return x * x +} + +let myArray = [0,1,2,3,4] +func through(array arr: [Int], f: (Int) -> (Int)) -> [Int] { + var result: [Int] = [] + for i in arr { + result.append(f(i)) + } + return result +} + + +let result = through(array: myArray) { + square($0) +} + + +//: [Escaping closure](@next) + + diff --git a/Swift/SwiftClosures.playground/Pages/Simple.xcplaygroundpage/Contents.swift b/Swift/SwiftClosures.playground/Pages/Simple.xcplaygroundpage/Contents.swift new file mode 100644 index 0000000..4a96940 --- /dev/null +++ b/Swift/SwiftClosures.playground/Pages/Simple.xcplaygroundpage/Contents.swift @@ -0,0 +1,23 @@ +var hiThere: () -> (String) = { + return "Hi there!" +} +hiThere() + + +var square: (Int) -> (Int) = { x in + return x * x +} + +/*: ### + Without explicitly declaring "x", you can use $0 as parameter: + */ +var squareAgain: (Int) -> (Int) = { + $0 * $0 +} +square(10) + +var newSquare = square + +newSquare(5) + +//: [Closure as function parameter](@next) diff --git a/Swift/SwiftClosures.playground/contents.xcplayground b/Swift/SwiftClosures.playground/contents.xcplayground new file mode 100644 index 0000000..a220fd5 --- /dev/null +++ b/Swift/SwiftClosures.playground/contents.xcplayground @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/Swift/SwiftClosures.playground/playground.xcworkspace/contents.xcworkspacedata b/Swift/SwiftClosures.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/Swift/SwiftClosures.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Swift/SwiftClosures.playground/playground.xcworkspace/xcuserdata/Askrav.xcuserdatad/UserInterfaceState.xcuserstate b/Swift/SwiftClosures.playground/playground.xcworkspace/xcuserdata/Askrav.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..411db5d Binary files /dev/null and b/Swift/SwiftClosures.playground/playground.xcworkspace/xcuserdata/Askrav.xcuserdatad/UserInterfaceState.xcuserstate differ