swift2 PromiseKit语法链swift

vsmadaxz  于 2022-11-06  发布在  Swift
关注(0)|答案(2)|浏览(184)

我尝试在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")
        }
    }
iecba09b

iecba09b1#

在块中不需要返回:

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

uxhixvfz2#

这里有很多问题。
1.你没有锁链,因为你没有回报你的承诺。
1.在第二个闭包中没有返回,这是编译错误,闭包说它返回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")
}

相关问题