2016-02-17 64 views
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) 
     } 


    }) 

} 

}

+0

任何運氣?你有什麼解決方案嗎? – NSIceCode

回答

2

要解決的第一個錯誤移動第一

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")) 
相關問題