HiveBrain v1.2.0
Get Started
← Back to all entries
patternswiftMinor

Fetching the definition of a word using a REST API

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
definitionrestthewordusingfetchingapi

Problem

This is the code I'm using to GET the definition of a word from Wordnik's REST API. I don't want to ignore any error but this is causing the code to be ridiculously long. I'm new to Swift so I'd appreciate it if someone could explain how best to trim it down.

```
class func define(params: DefinitionParameters!, delegate: WordnikDictionaryDelegate!){

if let url = url(definitionParams: params){

NSLog("URL: \(url)")

let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url, completionHandler: { (data, response, error) -> Void in

var error : NSError?
if let json = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &error) as? NSMutableArray{

NSLog("Response: \(json)")

if json.count == 0 {

delegate.dictionaryDidLoad(
nil,
forWord: params.word,
error: self.buildError(404, message: "No definitions found")
)

}else if let definitionJSON = json[0] as? NSMutableDictionary{

delegate.dictionaryDidLoad(
definitionJSON.valueForKeyPath("text") as? String,
forWord: params.word,
error: nil
)

}else{

delegate.dictionaryDidLoad(
nil,
forWord: params.word,
error: self.buildError(401, message: "Unexpected Result Format")
)

}

}else{

delegate.dictionaryDidLoad(
nil,
forWord: params.word,
error: self.buildError(401, message: "Unexpected results format")

Solution

Inside the if let json = ... { ... }, all the execution branches will call delegate.dictionaryDidLoad,
and the value of forWord is always params.word.
Instead, you could initialize value and error to nil,
set them in the appropriate conditional branches,
and finally call the delegate.
Something like this:

if let json = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &error) as? NSMutableArray{

    NSLog("Response: \(json)")

    if json.count == 0 {
        error = self.buildError(404, message: "No definitions found")
    } else if let definitionJSON = json[0] as? NSMutableDictionary{
        value = definitionJSON.valueForKeyPath("text") as? String,
    } else {
        error = self.buildError(401, message: "Unexpected Result Format")
    }
} else {
    error = self.buildError(401, message: "Unexpected Result Format")
}

delegate.dictionaryDidLoad(
    value,
    forWord: params.word,
    error: error
)

Code Snippets

if let json = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &error) as? NSMutableArray{

    NSLog("Response: \(json)")

    if json.count == 0 {
        error = self.buildError(404, message: "No definitions found")
    } else if let definitionJSON = json[0] as? NSMutableDictionary{
        value = definitionJSON.valueForKeyPath("text") as? String,
    } else {
        error = self.buildError(401, message: "Unexpected Result Format")
    }
} else {
    error = self.buildError(401, message: "Unexpected Result Format")
}

delegate.dictionaryDidLoad(
    value,
    forWord: params.word,
    error: error
)

Context

StackExchange Code Review Q#91513, answer score: 3

Revisions (0)

No revisions yet.