Advertisement
thieumao

Closure Example

Nov 10th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.78 KB | None | 0 0
  1.     func test() {
  2.         // c1
  3. //        request() { (json: String?, errorCode: Int?) in
  4. //            print("json", json!)
  5. //        }
  6.         // c2
  7. //        request(completion: {(json: String?, errorCode: Int?) in
  8. //            print(json!)
  9. //        })
  10.         // c3
  11.         let myVar2 : Completion  = { (json: String?, errorCode: Int?) -> () in
  12.             print("json ", json!)
  13.         }
  14.         request(completion: myVar2)
  15.        
  16.     }
  17.    
  18.     typealias Completion = (_ result: String?, _ errorCode: Int?) -> ()
  19.    
  20.     func request(completion: Completion? = nil) {
  21.         GET(completion: completion)
  22.     }
  23.    
  24.     private func GET(completion: Completion? = nil) {
  25.         if let callback = completion {
  26.             callback("Thieu Mao", 1)
  27.         }
  28.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement