1
我試圖使用Swift 2語法嘗試...做...捕獲錯誤處理,但試圖修改我的代碼後,我得到了一些我無法弄清楚的錯誤。在此先感謝您的幫助。錯誤遷移到Swift 2:從拋出函數類型到非拋出函數類型和使用未聲明類型的轉換無效
在dispatch_async行上,錯誤:從'()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)
}
})
}
}
任何運氣?你有什麼解決方案嗎? – NSIceCode