Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. //: Playground - noun: a place where people can play
  2.  
  3. import UIKit
  4. import XCPlayground
  5. import PlaygroundSupport
  6.  
  7. PlaygroundPage.current.needsIndefiniteExecution = true
  8.  
  9. var movie: String?;
  10. var actor1: String?;
  11. var actor2: String?;
  12.  
  13. var db = ["a1": "Abe", "a2": "Betty", "m1": "Ben Hur"]
  14.  
  15.  
  16. func printEvent() {
  17. print( actor1 ?? "unknown", ":",
  18. movie ?? "unknown",":",
  19. actor2 ?? "unknown")
  20. }
  21.  
  22. func apiCall(url: String, completion: @escaping (_ result: String) -> Void) {
  23. print("get apiCalled with url: " + url);
  24. DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
  25. // Put your code which should be executed with a delay here
  26.  
  27. if let result = db[url] {
  28. completion(result);
  29. }
  30. else {
  31. completion("error");
  32. }
  33. })
  34. }
  35.  
  36. func setActor1(result: String) {
  37. actor1 = result;
  38. apiCall(url: "m1", completion: setMovie)
  39. }
  40.  
  41. func setMovie(result: String) {
  42. movie = result;
  43. apiCall(url: "a2", completion: setActor2)
  44. }
  45.  
  46. func setActor2(result: String) {
  47. actor2 = result;
  48. printEvent()
  49. PlaygroundPage.current.finishExecution()
  50. }
  51.  
  52. apiCall(url: "a1", completion: setActor1)
  53.  
  54. print("After Setup");
  55. printEvent()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement