【发布时间】:2021-03-24 16:24:47
【问题描述】:
我正在尝试检查数组 newGames 是否包含 Game 类的某个对象。如果是这样,那么我不会将它添加到数组中。如果没有,我会添加它。我的代码如下:
for game in allGames{
if(!newGames.contains(game){
newGames.append(game)
}
}
allGames 是一个 Game 对象数组
var allGames = [Game(id: "5", title: "Dog", release_date: "1989", publisher: "Nintendo", price: "20", platform: "OS X", category: "Adventure", players: ["A", "B"]), Game(id: "5", title: "Dog", release_date: "1989", publisher: "Nintendo", price: "20", platform: "OS X", category: "Adventure", players: ["A", "B"])]
但是,我收到了错误 Cannot convert value of type 'Game' to expected argument type '(Game) throws -> Bool'
为什么会这样?我都试过了
for game in allGames{
if(!newGames.contains(where: game){
newGames.append(game)
}
}
和
for game in allGames{
if(!newGames.contains(game throws -> Bool){
newGames.append(game)
}
}
但似乎都没有解决错误,而只是增加了错误。发生了什么,为什么 swift 对 .contains(game) 方法不满意?
【问题讨论】:
-
没有人知道 allGames 是什么。
-
我已编辑问题以提供有关 allGames 的更多信息。这只是一系列 Game 类型的游戏
-
一个叫 newGames 的人是从哪里来的?
标签: ios arrays swift computer-science contains