Skip to content

Instantly share code, notes, and snippets.

@JRHeaton
Last active October 27, 2015 16:40
Show Gist options
  • Save JRHeaton/773c9de5b2ff6be9fb40 to your computer and use it in GitHub Desktop.
Save JRHeaton/773c9de5b2ff6be9fb40 to your computer and use it in GitHub Desktop.
Swift compiler does not detect the `try` expression given to the Result initializer unless broken up into its own expression
// warning: 'catch' block is unreachable because no errors are thrown in 'do' block
public func requestJSON<ModelType: MTLModel>(target: CocoaBlocAPI) -> SignalProducer<ModelType, NSError> {
return tryGetJSONObjectForKey(requestJSON(target), key: "data")
.attemptMap { (value: [NSObject:AnyObject]) -> Result<ModelType, NSError> in
do {
return Result(try MTLJSONAdapter.modelOfClass(ModelType.self, fromJSONDictionary: value) as! ModelType)
}
catch let error as NSError {
return .Failure(error)
}
}
}
// no warnings
public func requestJSON<ModelType: MTLModel>(target: CocoaBlocAPI) -> SignalProducer<ModelType, NSError> {
return tryGetJSONObjectForKey(requestJSON(target), key: "data")
.attemptMap { (value: [NSObject:AnyObject]) -> Result<ModelType, NSError> in
do {
let p = try MTLJSONAdapter.modelOfClass(ModelType.self, fromJSONDictionary: value) as! ModelType
return Result(p)
}
catch let error as NSError {
return .Failure(error)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment