2015-09-24 78 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) –

相关问题