Swift PromiseKitPromoises 模式库
Swift PromiseKit 是为 iOS 上实现的一个 Promoises 模式库,使用 Swift 编写。
示例代码:
CLLocationManager.promise().catch {
// If location cannot be determined, default to Chicago
return CLLocation(latitude: 41.89, longitude: -87.63)
}.then {
let (lat, lon) = ($0.coordinate.latitude, $0.coordinate.longitude)
return NSURLConnection.GET("http://user.net/\(lat)/\(lon)")
}.then { user in
let alert = UIAlertView()
alert.title = "Hi " + user["name"]
alert.addButtonWithTitle("Bye")
alert.addButtonWithTitle("Hi")
return alert.promise()
}.then { tappedButtonIndex -> Promise<Void>? in
if tappedButtonIndex == 0 {
return nil
}
let vc = HelloViewController()
return self.promiseViewController(vc).then { (modallyPresentedResult:String) -> Void in
//…
}
}.catch { error in
//…
}评论
