我怎么能在PromiseKit 3.0和Swift 2中链接承诺?我正在尝试以下代码:
let url = "https://httpbin/org/get"; let url2 = "https://httpbin/org/get"; firstly { NSURLSession.GET(url) }.then { (json: NSDictionary) in NSURLSession.GET(url2) // Error in this line }.then { (json: NSDictionary) in print("End of second request"); }
但我在第6行得到错误"无法将类型'URLDataPromise'的值转换为关闭结果类型'AnyPromise'".
这基本上是从PromiseKit网站检索的样本,它应该可以工作,不是吗?我究竟做错了什么?
我想到了.问题只是NSURLSession.GET没有返回NSDictionary,因此以下代码足以解决问题:
let url = "https://httpbin/org/get"; let url = "https://httpbin/org/get"; let url2 = "https://httpbin/org/get"; firstly { NSURLSession.GET(url) }.then { data in NSURLSession.GET(url2) }.then { data in print("End of second request"); }
一个重要的事情是,如果你的闭包有多行代码,Swift就无法推断出类型,如果是这种情况你必须在闭包声明中指定类型.这里举一些例子:
let url = "https://httpbin/org/get"; let url2 = "https://httpbin/org/get"; firstly { NSURLSession.GET(url) }.then { data -> URLDataPromise in print("Two lines clojure with return"); return NSURLSession.GET(url2) }.then { data -> Void in print("Two lines clojure without return"); print("Second line sample"); }