【问题标题】:PromiseKit Syntax Chain swiftPromiseKit 语法链 swift
【发布时间】:2015-10-26 19:44:25
【问题描述】:

我正在尝试在 Promise 工具包上链接一些 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")
        }
    }

【问题讨论】:

  • 请贴出你得到的编译错误。
  • 另外你的第二个块应该返回一个 Location 对象。它不返回任何东西。
  • AppDelegate.swift:43:15: Cannot invoke 'then' with an argument list of type '((Location) -&gt; Location)',如何返回位置,语法是什么

标签: swift swift2 promisekit


【解决方案1】:

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

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

【讨论】:

  • 我仍然有语法错误ViewController.swift:35:74: Cannot invoke 'then' with an argument list of type '((_) -&gt; _)'我正在使用promise kit 2.2.1
  • 你的 getLocation 方法返回 Promise&lt;Location&gt; 吗?
【解决方案2】:

这里有很多问题。

  1. 你没有链接,因为你没有兑现你的承诺。
  2. 您没有在第二个闭包中返回,这是编译错误,闭包说它正在返回 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")
}

【讨论】:

    猜你喜欢
    • 2017-07-08
    • 2015-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-11
    • 1970-01-01
    • 2021-05-12
    • 2016-01-06
    相关资源
    最近更新 更多