Flickr API call with Swift

I tried to get some JSON data from the Flickr API, but my Swift code is not working.
This is based on the Jameson Quave tutorial on creating an API request using Swift

func GetFlickrData(tags: String) {

    let baseURL = "https://api.flickr.com/services/rest/?&method=flickr.photos.search"
    let apiString = "&api_key=\(apiKey)"
    let searchString = "&tags=\(tags)"

    let requestURL = NSURL(string: baseURL + apiString + searchString)   
    let session = NSURLSession.sharedSession()

    let task = session.dataTaskWithURL(requestURL, completionHandler: { data, response, error -> Void in

        var result = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil) as NSDictionary

        dispatch_async(dispatch_get_main_queue(), {
            println(result)
        })
    })

    task.resume()
}

This shows me an error for the requestURL argument , which reads: "Vaue of type NSURL? Un unwrapped". When I add "!", The error disappears. But when making a call, I still get an error at runtime.

In the debugger, I see:

data = (NSData!) 16642 bytes
fatal error: unexpectedly found zero when deploying an optional value

What am I missing?

EDIT . This works fine on Xcode 6, it broke when upgrading Xcode to version 6.1

+4
1

NSDictionary? , , . JSONObjectWithData , :

class func JSONObjectWithData(data: NSData, options opt: NSJSONReadingOptions, error: NSErrorPointer) -> AnyObject?

NSDictionary? NSDictionary, .

let result = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil) as NSDictionary?
if let unwrappedResult = result {
    println(unwrappedResult)
}

SWIFT 2:

JSONObjectWithData , . :

public class func JSONObjectWithData(data: NSData, options opt: NSJSONReadingOptions) throws -> AnyObject

, do/catch , , ? try:

do {
    let result = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? NSDictionary
    print(result)
} catch {
    print(error)
}
+6
source

All Articles