2015-09-24 108 views
1

我寫了一個函數,該函數應該返回一個值,但值來自閉包。問題是,如果我試圖從閉包中返回一個值,它將它視爲來自完成處理程序的返回值。從包含閉包的Swift函數返回一個值

private func loadData() throws -> [Item] { 
    var items = [Item]() 
    let jsonUrl = "http://api.openweathermap.org/data/2.5/forecast/daily?units=metric&cnt=7&q=coventry,uk" 
    print(jsonUrl) 
    let session = NSURLSession.sharedSession() 
    guard let shotsUrl = NSURL(string: jsonUrl) else { 
     throw JSONError.InvalidURL(jsonUrl) 
    } 
    session.dataTaskWithURL(shotsUrl, completionHandler: {(data, response, error) -> Void in 
     do { 
      let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) 
      print(json) 
      guard let days:[AnyObject] = (json["list"] as! [AnyObject]) else { 
       throw JSONError.InvalidArray 
      } 
      for day in days { 
       guard let timestamp:Double = day["dt"] as? Double else { 
        throw JSONError.InvalidKey("dt") 
       } 
       print(timestamp) 
       let date = NSDate(timeIntervalSince1970: NSTimeInterval(timestamp)) 
       guard let weather:[AnyObject] = day["weather"] as? [AnyObject] else { 
        throw JSONError.InvalidArray 
       } 
       guard let desc:String = weather[0]["description"] as? String else { 
        throw JSONError.InvalidKey("description") 
       } 
       guard let icon:String = weather[0]["icon"] as? String else { 
        throw JSONError.InvalidKey("icon") 
       } 
       guard let url = NSURL(string: "http://openweathermap.org/img/w/\(icon).png") else { 
        throw JSONError.InvalidURL("http://openweathermap.org/img/w/\(icon).png") 
       } 
       guard let data = NSData(contentsOfURL: url) else { 
        throw JSONError.InvalidData 
       } 
       guard let image = UIImage(data: data) else { 
        throw JSONError.InvalidImage 
       } 
       guard let temp:AnyObject = day["temp"] else { 
        throw JSONError.InvalidKey("temp") 
       } 
       guard let max:Float = temp["max"] as? Float else { 
        throw JSONError.InvalidKey("max") 
       } 
       let newDay = Item(date: date, description: desc, maxTemp: max, icon: image) 
       print(newDay) 
       items.append(newDay) 
      } 
      return items // this line fails because I'm in the closure. I want this to be the value returned by the loadData() function. 
     } catch { 
      print("Fetch failed: \((error as NSError).localizedDescription)") 
     } 
    }) 
} 
+0

您可以傳遞閉包作爲回調或完成處理程序,您可以在要返回項目的位置調用該閉包程序。你需要寫出你願意如何處理你傳遞給loadData()函數的閉包中的數據。 – iamyogish

回答

0

添加完成處理程序(在我的例子命名dataHandler)到您的loadData功能:

private func loadData(dataHandler: ([Item])->()) throws { 
    var items = [Item]() 
    let jsonUrl = "http://api.openweathermap.org/data/2.5/forecast/daily?units=metric&cnt=7&q=coventry,uk" 
    print(jsonUrl) 
    let session = NSURLSession.sharedSession() 
    guard let shotsUrl = NSURL(string: jsonUrl) else { 
     throw JSONError.InvalidURL(jsonUrl) 
    } 
    session.dataTaskWithURL(shotsUrl, completionHandler: {(data, response, error) -> Void in 
     do { 
      let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) 
      print(json) 
      guard let days:[AnyObject] = (json["list"] as! [AnyObject]) else { 
       throw JSONError.InvalidArray 
      } 
      for day in days { 
       guard let timestamp:Double = day["dt"] as? Double else { 
        throw JSONError.InvalidKey("dt") 
       } 
       print(timestamp) 
       let date = NSDate(timeIntervalSince1970: NSTimeInterval(timestamp)) 
       guard let weather:[AnyObject] = day["weather"] as? [AnyObject] else { 
        throw JSONError.InvalidArray 
       } 
       guard let desc:String = weather[0]["description"] as? String else { 
        throw JSONError.InvalidKey("description") 
       } 
       guard let icon:String = weather[0]["icon"] as? String else { 
        throw JSONError.InvalidKey("icon") 
       } 
       guard let url = NSURL(string: "http://openweathermap.org/img/w/\(icon).png") else { 
        throw JSONError.InvalidURL("http://openweathermap.org/img/w/\(icon).png") 
       } 
       guard let data = NSData(contentsOfURL: url) else { 
        throw JSONError.InvalidData 
       } 
       guard let image = UIImage(data: data) else { 
        throw JSONError.InvalidImage 
       } 
       guard let temp:AnyObject = day["temp"] else { 
        throw JSONError.InvalidKey("temp") 
       } 
       guard let max:Float = temp["max"] as? Float else { 
        throw JSONError.InvalidKey("max") 
       } 
       let newDay = Item(date: date, description: desc, maxTemp: max, icon: image) 
       print(newDay) 
       items.append(newDay) 
      } 
      dataHandler(items) 
     } catch { 
      print("Fetch failed: \((error as NSError).localizedDescription)") 
     } 
    }).resume() 
} 

do { 
    try loadData { itemsArray in 
     print(itemsArray) 
    } 
} catch { 
    print(error) 
} 

我在操場測試,它的工作原理沒有錯誤:

enter image description here

+0

答案應該可以工作,但它會在操場上引發運行時錯誤(即使在包含結構和錯誤枚舉時也是如此)。然後我嘗試在課堂上公開該方法,並從我的視圖控制器調用它。沒有錯誤,但session.dataTaskWithURL回調無法運行。 –

+0

我已經添加了我的Playground運行我的答案沒有錯誤的屏幕截圖。請看一看。 //不要忘記,爲了在Playground中運行異步代碼,您需要啓用'XCPSetExecutionShouldContinueIndefinitely'! :) – Moritz

+0

道歉,忘了添加XCPSetExecutionShouldContinueIndefinitely(true) –

相關問題