0

I am trying to follow a tutorial - which failed and having found some code on stack over flow Make REST API call in Swift decided to try this approach instead. The following code is a copy and paste (I know!) with a change of URL yet I seem to be getting an error printed to the console rather than the JSON - the error being largely unhelpful - 0x0000000000000000

The JSON appears to print to the browser window so I am not fully sure what might be wrong with this approach? Please could someone provide some help as to why this might not be working?

Thanks

    var url : String = "http://www.flickr.com/services/rest/?method=flickr.test.echo&format=json&api_key=d6e995dee02d313a28ed4b799a09b869"
    var request : NSMutableURLRequest = NSMutableURLRequest()
    request.URL = NSURL(string: url)
    request.HTTPMethod = "GET"

    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler:{ (response:NSURLResponse!, data: NSData!, error: NSError!) -> Void in
        var error: AutoreleasingUnsafeMutablePointer<NSError?> = nil
        let jsonResult: NSDictionary! = NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers, error: error) as? NSDictionary

        if (jsonResult != nil) {
            println(jsonResult);
        } else {
            // couldn't load JSON, look at error
            println(error);
        }
    })
1
  • 1
    You're not passing that error object correctly. Define it as var error: NSError? and pass it with ..., error: &error); Swift will do the converting back and forth to a pointer for you. See Adopting Cocoa Design Patterns. Commented Oct 3, 2014 at 14:40

1 Answer 1

2

The problem is that the call returns data in JSONP not JSON

Your code would work ok if tested with a correct JSON like http://ip.jsontest.com

To make it work with Flickr you have to modify it as follows:

var json_str:NSString = NSString(data: data, encoding: NSUTF8StringEncoding)


json_str = json_str.substringFromIndex(14)
json_str = json_str.substringToIndex(json_str.length-1)

let new_data:NSData = json_str.dataUsingEncoding(NSUTF8StringEncoding)!


var error: AutoreleasingUnsafeMutablePointer<NSError?> = nil
let jsonResult: NSDictionary! = NSJSONSerialization.JSONObjectWithData(new_data, options:NSJSONReadingOptions.MutableContainers, error: error) as? NSDictionary

if (jsonResult != nil) {
    println(jsonResult);
} else {
    // couldn't load JSON, look at error
    println(error);
}
Sign up to request clarification or add additional context in comments.

2 Comments

You can also tell the Flickr API to send back straight JSON instead of JSONP by adding the parameter nojsoncallback=1 to the URL.
@abinop can you make this example a bit more clear and integrate it with his code? That'd be extremely helpful. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.