我正在尝试使用Swift 2语法来尝试...做...捕获错误处理,但是在尝试修改我的代码后,我遇到了一些我无法弄清的错误。在此先感谢您的帮助。
在dispatch_async行上,错误:从类型为(()的throwing函数的无效转换将throws->()'转换为非抛出函数类型为'dispatch_block_t'(aka'@convention(block)()->()')。
稍后在代码中,在“let 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)
}
})
}
}
最佳答案
要解决第一个错误,请先移动第一个
do {
直属
dispatch_async(queue
对于第二个错误:将代码更改为
let resultArray = resultDict.objectForKey("photos")!.objectForKey("photo") as! NSArray
同时用“as”替换每个“as”!
将searchURL声明更改为:
let searchURL = NSURL(string: FlickrHelper.URLForFlickrPhoto(flickrPhoto, size: "m"))
关于ios - 迁移到Swift 2时出错: Invalid conversion from throwing function of type to non-throwing function type and use of undeclared type,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35465226/