迁移到Swift 2的错误:从抛出类型的函数到非投掷函数类型的无效转换以及未声明类型的使用

时间:2016-02-17 18:52:25

标签: ios swift error-handling

我试图使用Swift 2语法进行尝试...执行...捕获错误处理,但在尝试修改我的代码后,我得到了一些错误,我可以&#39 ;弄清楚。在此先感谢您的帮助。

在dispatch_async行上,错误:投掷函数的转换无效'()throws - > ()'非投掷函数类型' dispatch_block_t' (又名' @convention(block)() - >()')。

稍后在代码中,在"让resultArray.NSArray" ...我收到错误"使用未声明的类型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)
        }


    })

}

}

1 个答案:

答案 0 :(得分:2)

要修复第一个错误,请移动第一个

do {

直接

dispatch_async(queue

第二个错误:将代码更改为

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

同时将每个"替换为"与" as!"

将searchURL声明更改为:

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