OctoberHammer
12/7/2017 - 3:26 PM

Playground

//: Playground - noun: a place where people can play

import UIKit
import Foundation
import PlaygroundSupport
import XCPlayground

class OneObject {
    var id: Int?
    var title: String?
    init(id: Int, title: String) {
        self.id = id
        self.title = title
    }
}

var dataSource: [OneObject] = [OneObject(id: 1, title: "First object"),
OneObject(id: 2, title: "Second object"),
OneObject(id: 3, title: "Third object"),
OneObject(id: 4, title: "Fourth object"),
OneObject(id: 5, title: "Fifth object"),]

func getListObjects(key: String, completion:  @escaping (Int, [OneObject], String) ->()) {
    print("\n")
    print("I'm gonna do a completion for this key: '\(key)'")
    print("But first, i'm doing a lot of code")
    print("And after all, I call a completion handler, HERE:")
    print("========================")
    completion(3, dataSource, "SomeString")
}

var firstCompletionCandidad: (Int, [OneObject], String) ->() = { (count: Int, listOfObjects: [OneObject], comment: String) in
    print("This code inside first completion handler: \(comment)")
    for i in count..<listOfObjects.count {
        print("\(listOfObjects[i]), \(i) ")
    }
}

var secondCompletionCandidad: (Int, [OneObject], String) ->() = { (count: Int, listOfObjects: [OneObject], comment: String) in
    print("It's a SECOND completionHandler: \(comment)")
    for i in 0..<count {
        print("\(listOfObjects[i]), \(i) ")
    }
}

getListObjects(key: "First", completion: firstCompletionCandidad)
getListObjects(key: "Second", completion: secondCompletionCandidad)