PromiseKit语法链swift

时间:2015-10-26 19:44:26

标签: swift swift2 promisekit

我正试图在promise kit上链接一些承诺,当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")
        }
    }

2 个答案:

答案 0 :(得分:0)

你的街区不需要退货:

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

答案 1 :(得分:0)

这里有很多问题。

  1. 你没有链接,因为你没有回复你的诺言。
  2. 你没有在第二个闭包中返回,这是编译错误,闭包说它返回Location但闭包返回Void
  3. 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")
    }