1

promise キットでいくつかの promise をチェーンしようとしています。promise の型が this のような場合、構文の問題があります。promise に型があるPromise<Location>場合にのみコンパイラ エラーが発生します。promisekit の使用は初めてです

Swift.start(host,"","").then{ result -> Void in

    }.then{ obj -> Void in
        println(obj)
        Swift.getCurrent.then{ obj -> Void in
            let payload:Dictionary<String,AnyObject> = obj as! Dictionary<String,AnyObject>
            self.deviceUUID = payload["uuid"] as! String

        }
    }.then { obj -> Location in
        println(obj)
        Swift.getLocation("3333").then{ location in
            self.locationUUID = location.get("uuid")
        }
    }
4

2 に答える 2

0

ここにはいくつかの問題があります。

  1. 約束を返していないので、連鎖していません。
  2. 2 番目のクロージャで返されていません。これはコンパイル エラーです。クロージャは a を返すと言っていLocationますが、クロージャは戻りますVoid

.

Swift.start(host, "", "").then { result -> Void in

}.then { obj -> Promise<Something> in
    print(obj)

    // I changed the return of this closure, see above
    // we must return the promise or we are not chaining
    return Swift.getCurrent.then { obj -> Void in
        let payload: Dictionary<String, AnyObject> = obj as! Dictionary<String, AnyObject>
        self.deviceUUID = payload["uuid"] as! String

    }
}.then { obj -> Location in
    println(obj)

    // we promised a return value above, so we must return
    return Swift.getLocation("3333").then { location in
        self.locationUUID = location.get("uuid")
    }
}

しかし、あなたのコードを見ると、間違っているように見えます。これは実際にあなたが求めているものですか?

firstly { _ -> Promise<[String: AnyObject]> in
    return Swift.getCurrent
}.then { payload -> Promise<Location> in
    self.deviceUUID = payload["uuid"] as! String
    return Swift.getLocation("3333")
}.then { location -> Void in
    self.locationUUID = location.get("uuid")
}
于 2016-08-04T18:57:25.273 に答える