SwiftPromiseKit是为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("https://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 //…}
评论