Advertisement
Guest User

Untitled

a guest
May 4th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. extension NSURLSession {
  2.  
  3. enum UTF8Encoding : ErrorType { case Error }
  4. enum JsonDecoding : ErrorType { case Error }
  5.  
  6. func dataForRequest(request:NSURLRequest, completeWith: (dataResult: () throws -> NSData) -> Void) {
  7.  
  8. let task = self.dataTaskWithRequest(request) { (data:NSData?, response:NSURLResponse?, error:NSError?) in
  9.  
  10. guard let data = data else {
  11.  
  12. completeWith(dataResult: { throw error! } )
  13. return
  14. }
  15.  
  16. completeWith(dataResult: { return data } )
  17. }
  18.  
  19. task.resume()
  20. }
  21.  
  22. func textForRequest(request:NSURLRequest, completeWith: (textResult: () throws -> String) -> Void) {
  23.  
  24. let task = self.dataTaskWithRequest(request) { (data:NSData?, response:NSURLResponse?, error:NSError?) in
  25.  
  26. guard let data = data else {
  27.  
  28. completeWith(textResult: { throw error! } )
  29. return
  30. }
  31.  
  32. completeWith(textResult: {
  33.  
  34. guard let text = String(data: data, encoding: NSUTF8StringEncoding) else {
  35. throw UTF8Encoding.Error
  36. }
  37.  
  38. return text
  39. })
  40. }
  41.  
  42. task.resume()
  43. }
  44.  
  45. func jsonForRequest(request:NSURLRequest, completeWith: (jsonResult: () throws -> [String:AnyObject]) -> Void) {
  46.  
  47. let task = self.dataTaskWithRequest(request) { (data:NSData?, response:NSURLResponse?, error:NSError?) in
  48.  
  49. guard let data = data else {
  50.  
  51. completeWith(jsonResult: { throw error! } )
  52. return
  53. }
  54.  
  55. completeWith(jsonResult: {
  56.  
  57. let json = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) as? [String:AnyObject]
  58.  
  59. if json == nil {
  60. throw JsonDecoding.Error
  61. }
  62.  
  63. return json!
  64. })
  65. }
  66.  
  67. task.resume()
  68. }
  69.  
  70. func awaitDataForRequest(request:NSURLRequest) throws -> NSData {
  71.  
  72. let wait: dispatch_semaphore_t = dispatch_semaphore_create(0);
  73. var resData:NSData?
  74. var resError:NSError?
  75.  
  76. let task = self.dataTaskWithRequest(request) { (data:NSData?, response:NSURLResponse?, error:NSError?) in
  77.  
  78. resData = data
  79. resError = error
  80.  
  81. dispatch_semaphore_signal(wait)
  82. }
  83.  
  84. task.resume()
  85.  
  86. dispatch_semaphore_wait(wait, DISPATCH_TIME_FOREVER)
  87.  
  88. guard let data = resData else {
  89. throw resError!
  90. }
  91.  
  92. return data
  93. }
  94.  
  95. func awaitTextForRequest(request:NSURLRequest) throws -> String {
  96.  
  97. let data = try self.awaitDataForRequest(request)
  98.  
  99. guard let text = String(data: data, encoding: NSUTF8StringEncoding) else {
  100. throw UTF8Encoding.Error
  101. }
  102.  
  103. return text
  104. }
  105.  
  106. func awaitJsonWithRequest(request:NSURLRequest) throws -> [String:AnyObject] {
  107.  
  108. let data = try self.awaitDataForRequest(request)
  109.  
  110. let json = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) as? [String:AnyObject]
  111.  
  112. if json == nil {
  113. throw JsonDecoding.Error
  114. }
  115.  
  116. return json!
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement