私はSwiftの約束を新しくし、PromiseKitを使用して、それを使用しようとします。私は次のエラーを取得しかしエラー:Swift + PromiseKitを使用して '() - >()'型の値を 'String'型で閉じることはできません
//: Playground - noun: a place where people can play
import UIKit
import PromiseKit
func foo(_ error: Bool) -> Promise<String> {
return Promise { fulfill, reject in
if (!error) {
fulfill("foo")
} else {
reject(Error(domain:"", code:1, userInfo:nil))
}
}
}
foo(true).then { response -> String in {
print(response)
}
}
:私は、次のコードを持っているあなたはthen
に渡しているクロージャはString
を返すように主張しているので、
error: MyPlayground.playground:11:40: error: cannot convert value of type '() ->()' to closure result type 'String' foo(true).then { response -> String in {
感謝を。私は十分に約束を理解していないようだ。もしresponse - > Voidならば、fooは期待どおりに動作することを意味します - それで私は応答として "foo"にアクセスしますか? –
はい。 ' - >'の後の型は、もし何かがあれば、クロージャから何が戻ってくるかだけを示します。 '応答 'は変更されません。 – aaplmath
私は遊び場でammendmentsも試しましたが、 "foo"は表示されません。代わりに、右側に「Promise:UnsealedState」と書かれていますか? –