我正在嘗試洗掉陣列“甲板”中的 3 個黃色物件。該陣列由 Cards 物件組成。我用了:
var counter = 3
var newArr = arr.filter {
if counter > 0, $0 == 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 張黃牌,然后將它們從牌組中移除?
uj5u.com熱心網友回復:
==在 上定義Equatable,您的Cards型別不是。您要么需要使Cards符合 equatable,并決定您的哪些屬性算作“相等”(相同型別?相同收入?兩者?影像呢?),或者直接比較您關心的屬性。
uj5u.com熱心網友回復:
正如@jrturton 指出您的Cards型別不符合Equatable協議,因此您不能使用==運算子在其兩個實體之間進行比較。
此外,您發布的代碼與您的潛在問題并不真正匹配:“如何檢查牌組是否有 3 張黃牌,然后將它們從牌組中洗掉?”
在您的代碼中,即使只包含 1張黃牌,您也只需洗掉黃牌。
所以要回答你真正的問題,你應該首先檢查牌組是否至少包含 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 { $0.type == "yellow" }
}
}
但是當然這個解決方案只適用于牌組中的前 3張黃牌。你真正需要達到什么目標?也許洗掉所有 三張黃牌?那么你不妨采用另一種方法:
var yellowsCount = 0
let newDeck: Array<Cards> = deck.reduce([], {
if $1.type == "yellow" {
yellowsCount = 1
guard
yellowsCount < 3
else {
yellowsCount = 0
return $0.filter { c in c.type != "yellow" }
}
}
return $0 [$1]
}
以上所有內容均基于您打算檢查該type物業Cards的yellow價值這一事實。無論如何,我也推薦一種與對該屬性使用基于字串的方法不同的方法,而是enum Color為該值使用嵌套:
struct Cards {
enum Color: CaseIterable {
case yellow, blue, darkBlue, red, green
}
var type: Color
var income: Int
var images: Array<UIImage>
}
通過這種方式,您將type通過采用switch或if case let構造對屬性進行比較:兩者都比基于字串的比較(錯別字等)更不容易出錯。
此外,使用上述方法,如果您確定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 […]
…
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/354274.html
上一篇:嗨,伙計們,我如何在反應組件上呈現這些成分而不必寫出“strIngredient1”到“strIngredient20”
下一篇:每盒包含多個資訊的陣列
