jacquelion
jacquelion

Reputation: 159

Errors migrating to Swift 2: Invalid conversion from throwing function of type to non-throwing function type and use of undeclared type

I'm trying to use the Swift 2 syntax for try... do... catch for error handling, but after trying to modify my code I'm getting a couple errors that I can't figure out. Thanks in advance for your help.

On the dispatch_async line, the error: Invalid conversion from throwing function of type '() throws -> ()' to non-throwing function type 'dispatch_block_t' (aka '@convention(block) () -> ()').

Later in the code, on "let resultArray.NSArray"... I am getting the error "use of undeclared type NSArray".

func searchFlickrForString(searchStr:String, completion:(searchString:String!, FlickrPhotos:NSMutableArray!, error:NSError!)->()){
    let searchURL:String = FlickrHelper.URLForSearchString(searchStr)

    let queue:dispatch_queue_t = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)

    dispatch_async(queue, {

        var error:NSError?

        let searchResultString:String! = try String(contentsOfURL: NSURL(fileURLWithPath: searchURL), encoding: NSUTF8StringEncoding)
        do {
            // Parse JSON Response
            let jsonData:NSData! = searchResultString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)

            let resultDict:NSDictionary! = try NSJSONSerialization.JSONObjectWithData(jsonData, options: []) as! NSDictionary
            do {
                let status:String = resultDict.objectForKey("stat") as! String
                if status ==  "fail" {
                    let error:NSError? = NSError(domain: "FlickrSearch", code: 0, userInfo: [NSLocalizedFailureReasonErrorKey:resultDict.objectForKey("message")!])

                    completion(searchString: searchStr, FlickrPhotos:  nil, error: error)
                } else {
                    let resultArray:NSArrray = resultDict.objectForKey("photos").objectForKey("photo") as NSArray

                    let flickrPhotos:NSMutableArray = NSMutableArray()

                    for photoObject in resultArray{
                        let photoDict:NSDictionary = photoObject as NSDictionary

                        var flickrPhoto:FlickrPhoto = FlickrPhoto()
                        flickrPhoto.farm = photoDict.objectForKey("farm") as Int
                        flickrPhoto.server = photoDict.objectForKey("farm") as String
                        flickrPhoto.secret = photoDict.objectForKey("farm") as String
                        flickrPhoto.photoID = photoDict.objectForKey("farm") as String

                        let searchURL:String = FlickrHelper.URLForFlickrPhoto(flickrPhoto, size: "m")
                        //download image
                        let imageData:NSData = NSData(contentsOfURL: searchURL, options: nil)

                    }
                }
            } catch let error as NSError{
                completion(searchString: searchStr, FlickrPhotos: nil, error: error)
            }
        } catch {
            //completion(searchString: searchStr, FlickrPhotos: nil, error: error)
        }


    })

}

}

Upvotes: 1

Views: 750

Answers (1)

Stefan
Stefan

Reputation: 5451

To fix the first error move the first

do {

directly under

dispatch_async(queue

For your second error: Change your code to

let resultArray = resultDict.objectForKey("photos")!.objectForKey("photo") as! NSArray

Also replace every "as" with "as!"

Change the searchURL declaration to:

let searchURL = NSURL(string: FlickrHelper.URLForFlickrPhoto(flickrPhoto, size: "m"))

Upvotes: 2

Related Questions