getting error: Binary operator '==' cannot be applied to two 'x' operands, how to remove certain number of elements in this array of objects
getting error: Binary operator '==' cannot be applied to two 'x' operands, how to remove certain number of elements in this array of objects
我正在尝试删除阵列中的 3 个黄色对象 'deck'。该数组由 Cards 对象组成。我用过:
var counter = 3
var newArr = arr.filter {
if counter > 0, [=10=] == yellow {
counter -= 1
return false
}
return true
}
我收到错误:二元运算符“==”不能应用于两个卡片操作数
我有一个结构:
import UIKit
struct Cards {
var type: String
var income: Int
var images: [UIImage]
init(type: String, income: Int, images: [UIImage]) {
self.type = type
self.income = income
self.images = images
}
}
let yellow = Cards(type: "yellow", income: 0, images: [#imageLiteral(resourceName: "yellow1"), #imageLiteral(resourceName: "yellow2")])
let darkBlue = Cards(type: "dark blue", income: 2, images: [#imageLiteral(resourceName: "dark1"), #imageLiteral(resourceName: "dark2")])
let red = Cards(type: "red", income: 2, images: [#imageLiteral(resourceName: "red1"), #imageLiteral(resourceName: "red2")])
let green = Cards(type: "green", income: 1, images: [#imageLiteral(resourceName: "green1"), #imageLiteral(resourceName: "green2")])
let blue = Cards(type: "blue", income: 3, images: [#imageLiteral(resourceName: "blue1"), #imageLiteral(resourceName: "blue2")])
我有一个数组 deck = [Cards],它是我创建的,然后填充了我创建的生成器,它使七张卡片中的前 5 张变成黄色。当我在控制台中打印牌组时,它显示为:
[game.Cards(type: "yellow", income: 0, images: [<UIImage:0x6000037a8cf0 named(main: yellow1) {416.66666666666669, 583.33333333333337}>, <UIImage:0x6000037a8e10 named(main: yellow2) {416.66666666666669, 583.33333333333337}>]), outthegame.Cards(type: "yellow", income: 0, images: [<UIImage:0x6000037a8cf0 named(main: yellow1) {416.66666666666669, 583.33333333333337}>, <UIImage:0x6000037a8e10 named(main: yellow2) {416.66666666666669, 583.33333333333337}>]), outthegame.Cards(type: "yellow", income: 0, images: [<UIImage:0x6000037a8cf0 named(main: yellow1) {416.66666666666669, 583.33333333333337}>, <UIImage:0x6000037a8e10 named(main: yellow2) {416.66666666666669, 583.33333333333337}>]), outthegame.Cards(type: "yellow", income: 0, images: [<UIImage:0x6000037a8cf0 named(main: yellow1) {416.66666666666669, 583.33333333333337}>, <UIImage:0x6000037a8e10 named(main: yellow2) {416.66666666666669, 583.33333333333337}>]), outthegame.Cards(type: "yellow", income: 0, images: [<UIImage:0x6000037a8cf0 named(main: yellow1) {416.66666666666669, 583.33333333333337}>, <UIImage:0x6000037a8e10 named(main: yellow2) {416.66666666666669, 583.33333333333337}>]), outthegame.Cards(type: "blue", income: 3, images: [<UIImage:0x6000037a9170 named(main: blue1) {416.66666666666669, 583.33333333333337}>, <UIImage:0x6000037a8f30 named(main: blue2) {416.66666666666669, 583.33333333333337}>]), outthegame.Cards(type: "green", income: 1, images: [<UIImage:0x6000037a0ab0 named(main: green1) {416.66666666666669, 583.33333333333337}>, <UIImage:0x6000037a0bd0 named(main: green2) {416.66666666666669, 583.33333333333337}>])]
如何检查牌组是否有 3 张黄牌,然后将它们从牌组中移除?
==
是在 Equatable
上定义的,您的 Cards
类型不是。您要么需要使 Cards
符合 equatable,并决定您的哪些属性计入“相等”(相同类型?相同收入?两者?图片呢?),或者直接比较您的属性关心
正如@jrturton 所指出的,您的 Cards
类型不符合 Equatable
协议,因此您不能使用 ==
运算符来比较其两个实例.
此外,您发布的代码与您的基本问题不符:“如何检查牌组是否有 3 张黄牌,然后将它们从牌组中移除?”
在您的代码中,您只是删除了 yellow cards,即使它只包含 1 张 yellow card。
所以为了回答你真正的问题,你应该首先检查一副牌是否包含至少 3 张 黄牌 ,然后移除前 3 张 黄牌 :
var yellowsCount = 0
for card in deck where card.type == "yellow" {
yellowsCount += 1
guard yellowsCount < 3 else { break }
}
if yellowsCount == 3 {
for _ in 1...3 {
deck.removeFirst { [=10=].type == "yellow" }
}
}
当然,这个解决方案只适用于牌组中的前 3 张 黄牌 。你真正需要达到的目标是什么?也许删除所有 三张黄牌?那你不妨换一种方式:
var yellowsCount = 0
let newDeck: Array<Cards> = deck.reduce([], {
if .type == "yellow" {
yellowsCount += 1
guard
yellowsCount < 3
else {
yellowsCount = 0
return [=11=].filter { c in c.type != "yellow" }
}
}
return [=11=] + []
}
以上所有内容都是基于您打算检查 Cards
上的 type
属性 的 yellow
值这一事实。
无论如何,我也推荐一种不同于使用基于字符串的方法的方法 属性,而是使用嵌套的 enum
Color
作为该值:
struct Cards {
enum Color: CaseIterable {
case yellow, blue, darkBlue, red, green
}
var type: Color
var income: Int
var images: Array<UIImage>
}
通过这种方式,您将通过采用 switch
或 if case let
结构对 type
属性 进行比较:两者都比基于字符串的比较更不容易出错(错别字等)。
此外,使用上述方法,如果您决定 Card
的某些 type
始终具有相同的图像,您可以将 images
属性 转换为计算 属性 在嵌套的 enum
上,如:
extension Color {
var images: Array<UIImage> {
switch self {
case .yellow: return [yellowP1, yellowP2]
case .green: return [greenP1, greenP2, greenP3]
case .blue: return […]
…
}
}
}
我正在尝试删除阵列中的 3 个黄色对象 'deck'。该数组由 Cards 对象组成。我用过:
var counter = 3
var newArr = arr.filter {
if counter > 0, [=10=] == yellow {
counter -= 1
return false
}
return true
}
我收到错误:二元运算符“==”不能应用于两个卡片操作数
我有一个结构:
import UIKit
struct Cards {
var type: String
var income: Int
var images: [UIImage]
init(type: String, income: Int, images: [UIImage]) {
self.type = type
self.income = income
self.images = images
}
}
let yellow = Cards(type: "yellow", income: 0, images: [#imageLiteral(resourceName: "yellow1"), #imageLiteral(resourceName: "yellow2")])
let darkBlue = Cards(type: "dark blue", income: 2, images: [#imageLiteral(resourceName: "dark1"), #imageLiteral(resourceName: "dark2")])
let red = Cards(type: "red", income: 2, images: [#imageLiteral(resourceName: "red1"), #imageLiteral(resourceName: "red2")])
let green = Cards(type: "green", income: 1, images: [#imageLiteral(resourceName: "green1"), #imageLiteral(resourceName: "green2")])
let blue = Cards(type: "blue", income: 3, images: [#imageLiteral(resourceName: "blue1"), #imageLiteral(resourceName: "blue2")])
我有一个数组 deck = [Cards],它是我创建的,然后填充了我创建的生成器,它使七张卡片中的前 5 张变成黄色。当我在控制台中打印牌组时,它显示为:
[game.Cards(type: "yellow", income: 0, images: [<UIImage:0x6000037a8cf0 named(main: yellow1) {416.66666666666669, 583.33333333333337}>, <UIImage:0x6000037a8e10 named(main: yellow2) {416.66666666666669, 583.33333333333337}>]), outthegame.Cards(type: "yellow", income: 0, images: [<UIImage:0x6000037a8cf0 named(main: yellow1) {416.66666666666669, 583.33333333333337}>, <UIImage:0x6000037a8e10 named(main: yellow2) {416.66666666666669, 583.33333333333337}>]), outthegame.Cards(type: "yellow", income: 0, images: [<UIImage:0x6000037a8cf0 named(main: yellow1) {416.66666666666669, 583.33333333333337}>, <UIImage:0x6000037a8e10 named(main: yellow2) {416.66666666666669, 583.33333333333337}>]), outthegame.Cards(type: "yellow", income: 0, images: [<UIImage:0x6000037a8cf0 named(main: yellow1) {416.66666666666669, 583.33333333333337}>, <UIImage:0x6000037a8e10 named(main: yellow2) {416.66666666666669, 583.33333333333337}>]), outthegame.Cards(type: "yellow", income: 0, images: [<UIImage:0x6000037a8cf0 named(main: yellow1) {416.66666666666669, 583.33333333333337}>, <UIImage:0x6000037a8e10 named(main: yellow2) {416.66666666666669, 583.33333333333337}>]), outthegame.Cards(type: "blue", income: 3, images: [<UIImage:0x6000037a9170 named(main: blue1) {416.66666666666669, 583.33333333333337}>, <UIImage:0x6000037a8f30 named(main: blue2) {416.66666666666669, 583.33333333333337}>]), outthegame.Cards(type: "green", income: 1, images: [<UIImage:0x6000037a0ab0 named(main: green1) {416.66666666666669, 583.33333333333337}>, <UIImage:0x6000037a0bd0 named(main: green2) {416.66666666666669, 583.33333333333337}>])]
如何检查牌组是否有 3 张黄牌,然后将它们从牌组中移除?
==
是在 Equatable
上定义的,您的 Cards
类型不是。您要么需要使 Cards
符合 equatable,并决定您的哪些属性计入“相等”(相同类型?相同收入?两者?图片呢?),或者直接比较您的属性关心
正如@jrturton 所指出的,您的 Cards
类型不符合 Equatable
协议,因此您不能使用 ==
运算符来比较其两个实例.
此外,您发布的代码与您的基本问题不符:“如何检查牌组是否有 3 张黄牌,然后将它们从牌组中移除?”
在您的代码中,您只是删除了 yellow cards,即使它只包含 1 张 yellow card。
所以为了回答你真正的问题,你应该首先检查一副牌是否包含至少 3 张 黄牌 ,然后移除前 3 张 黄牌 :
var yellowsCount = 0
for card in deck where card.type == "yellow" {
yellowsCount += 1
guard yellowsCount < 3 else { break }
}
if yellowsCount == 3 {
for _ in 1...3 {
deck.removeFirst { [=10=].type == "yellow" }
}
}
当然,这个解决方案只适用于牌组中的前 3 张 黄牌 。你真正需要达到的目标是什么?也许删除所有 三张黄牌?那你不妨换一种方式:
var yellowsCount = 0
let newDeck: Array<Cards> = deck.reduce([], {
if .type == "yellow" {
yellowsCount += 1
guard
yellowsCount < 3
else {
yellowsCount = 0
return [=11=].filter { c in c.type != "yellow" }
}
}
return [=11=] + []
}
以上所有内容都是基于您打算检查 Cards
上的 type
属性 的 yellow
值这一事实。
无论如何,我也推荐一种不同于使用基于字符串的方法的方法 属性,而是使用嵌套的 enum
Color
作为该值:
struct Cards {
enum Color: CaseIterable {
case yellow, blue, darkBlue, red, green
}
var type: Color
var income: Int
var images: Array<UIImage>
}
通过这种方式,您将通过采用 switch
或 if case let
结构对 type
属性 进行比较:两者都比基于字符串的比较更不容易出错(错别字等)。
此外,使用上述方法,如果您决定 Card
的某些 type
始终具有相同的图像,您可以将 images
属性 转换为计算 属性 在嵌套的 enum
上,如:
extension Color {
var images: Array<UIImage> {
switch self {
case .yellow: return [yellowP1, yellowP2]
case .green: return [greenP1, greenP2, greenP3]
case .blue: return […]
…
}
}
}