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);
        }
    })
Community
  • 1
  • 1
Biscuit128
  • 5,218
  • 22
  • 89
  • 149
  • 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](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/AdoptingCocoaDesignPatterns.html#//apple_ref/doc/uid/TP40014216-CH7-XID_7). – Mike S Oct 03 '14 at 14:40

1 Answers1

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);
}
abinop
  • 3,153
  • 5
  • 32
  • 46
  • You can also tell the Flickr API to send back straight JSON instead of JSONP by adding the parameter `nojsoncallback=1` to the URL. – Mike S Oct 03 '14 at 14:27
  • @abinop can you make this example a bit more clear and integrate it with his code? That'd be extremely helpful. Thank you. – ChainFuse Nov 04 '14 at 04:35