1

我试图在 Promise Kit 上链接一些 Promise,当 Promise 类型是这样时我有语法问题Promise<Location>,只有当 Promise 有类型时,我才会得到编译器错误。我是使用 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. 您没有在第二个闭包中返回,这是编译错误,闭包说它正在返回 aLocation但闭包返回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 回答
0

您不需要在您的块中返回:

.then { obj -> Location in
      Swift.getLocation("433434").then{ location in
          self.locationUUID = location.get("uuid")
      }
}
于 2015-10-26T21:15:54.207 回答