Skip to content

Commit

Permalink
fix type of completion handler to avoid use of IUO
Browse files Browse the repository at this point in the history
  • Loading branch information
ishkawa committed Mar 9, 2015
1 parent 4207801 commit b006e77
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions APIKit/APIKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ private extension NSURLSessionDataTask {
}
}

private var completionHandler: ((NSData!, NSURLResponse!, NSError!) -> Void)? {
private var completionHandler: ((NSData?, NSURLResponse?, NSError?) -> Void)? {
get {
return (objc_getAssociatedObject(self, &dataTaskCompletionHandlerKey) as? Box<(NSData!, NSURLResponse!, NSError!) -> Void>)?.unbox
return (objc_getAssociatedObject(self, &dataTaskCompletionHandlerKey) as? Box<(NSData?, NSURLResponse?, NSError?) -> Void>)?.unbox
}

set {
Expand Down Expand Up @@ -167,18 +167,24 @@ public class API: NSObject, NSURLSessionDelegate, NSURLSessionDataDelegate {
dispatch_async(mainQueue, { handler(.Failure(Box(error))) })
return
}

let mappedResponse: Result<T.Response, NSError> = self.responseBodyParser().parseData(data).flatMap { rawResponse in
if let response = request.responseFromObject(rawResponse) {
return success(response)
} else {
let userInfo = [NSLocalizedDescriptionKey: "failed to create model object from raw object."]
let error = NSError(domain: APIKitErrorDomain, code: 0, userInfo: userInfo)
return failure(error)

if let data = data {
let mappedResponse: Result<T.Response, NSError> = self.responseBodyParser().parseData(data).flatMap { rawResponse in
if let response = request.responseFromObject(rawResponse) {
return success(response)
} else {
let userInfo = [NSLocalizedDescriptionKey: "failed to create model object from raw object."]
let error = NSError(domain: APIKitErrorDomain, code: 0, userInfo: userInfo)
return failure(error)
}

}

dispatch_async(mainQueue, { handler(mappedResponse) })
} else {
let userInfo = [NSLocalizedDescriptionKey: "unable to get response body despite NSURLSession raised no error."]
let error = NSError(domain: APIKitErrorDomain, code: 0, userInfo: userInfo)
dispatch_async(mainQueue, { handler(.Failure(Box(error))) })
}
dispatch_async(mainQueue, { handler(mappedResponse) })
}

task.resume()
Expand Down

0 comments on commit b006e77

Please sign in to comment.