无法将类型 'Promise<[NSDictionary]>' 的值转换为预期的参数类型 'Guarantee<Void>'

Cannot convert value of type 'Promise<[NSDictionary]>' to expected argument type 'Guarantee<Void>'

目前,当我将我的 PromiseKit 从 Swift 2 更新到 Swift 4 时遇到问题。我的很多 Promise 都将这个问题从 NSDictionary 返回到 Guarantee Void。我不明白为什么会出现这个问题。如果有人能向我解释一下,那将不胜感激!

func getPlayerGameLog(sportName: String) -> Promise<NSArray> {
    let team = (self as? PlayerWithPositionAndGame)?.team
    let position = (self as? PlayerWithPositionAndGame)?.position

    if sportName == "mlb" {
        return when(Data.mlbPlayerGameLogs[id].get(), Data.sportsTeams[sportName].get()).then { (result, teams) -> NSArray in
            if result.count == 0 {
                return []
            } else {
                // Omitted Code
                return gameStats
            }
        }
    } else if sportName == "nfl" {
        return when(Data.nflPlayerGameLogs[id].get(), Data.sportsTeams[sportName].get()).then { (result, teams) -> NSArray in
            if result.count == 0 {
                return []
            } else {
                // Omitted Code
                return gameStats
            }
        }
    } else {
        return Data.nbaPlayerGameLogs[id].get().then { gameLogs in
            return []
        }
    }
}

现游戏日志声明如下

static let mlbPlayerGameLogs = MultiCache { id in API.getMLBPlayerGameLogs(playerID: id) }

mlb/nfl/nba也一样。

这个函数的目的很简单。它只是将团队信息拉入函数并返回它。

"return when()" 行出现错误

首先,它是 swift 4,你不应该使用 NS... 类型,现在有纯 swift 类型。

接下来,根据release notes

  • then is fed the previous promise value and requires you return a promise.
  • done is fed the previous promise value and returns a Void promise (which is 80% of chain usage)
  • map is fed the previous promise value and requires you return a non-promise, ie. a value.

几乎所有的then都应该换成map

此外,您应该将 when 重写为 when(fulfilled:) 为 return Promise,而不是 Guarantee