gpt4 book ai didi

Swift 字典默默地不存储新项目

转载 作者:行者123 更新时间:2023-11-30 11:27:09 26 4
gpt4 key购买 nike

我需要一些建议。这是代码。我试图在删除其中一些项目后将其添加到词典中。但一切都没有改变。为什么?我哪里错了?

这个结构就像实现了 Equatable 协议(protocol)的演示。

struct Card: Equatable {
static func == (lhs: Card, rhs: Card) -> Bool {
return
(lhs.color == rhs.color ||
lhs.number == rhs.number ||
lhs.shading == rhs.shading ||
lhs.symbol == rhs.symbol)
}
var number = 0
var symbol = 0
var shading = 0
var color = 0

var isSelected = false
var isSet = false
}

这是一个纸牌游戏模型,在本例中只是作为演示来展示

struct gameSet {
var cards = [Card]()
var cardField = [Int:Card]()

init () {
for num in 1...3 {
for sym in 1...3 {
for sha in 1...3 {
for col in 1...3 {
cards += [Card(number: num, symbol: sym, shading: sha, color: col, isSelected: false, isSet: false)]
}
}
}
}
}
}

Int 获取随机数的扩展

extension Int {
var random: Int {
return Int(arc4random_uniform(UInt32(self)))
}
}
var game = gameSet()

// cardField gets 24 random items from game.cards

var cardCount = 24

repeat {
let randomCard = game.cards[(game.cards.count-1).random]
game.cardField[game.cardField.count] = randomCard
game.cards.remove(at: game.cards.index(of: randomCard)!)
cardCount -= 1
} while cardCount != 0

// we got 24 items in cardField dictionary
print(game.cardField.count)

// delete 3 random items from cardField

for _ in 1...3 {
let randomKey = game.cardField.count.random
game.cardField.removeValue(forKey: randomKey)
}

// we got 21 items in cardField dictionary, ok
print(game.cardField.count)

// let's add 3 another items to cardField dictionary same way
var cardCount2 = 3

repeat {
let randomCard = game.cards[(game.cards.count-1).random]
game.cardField[game.cardField.count] = randomCard
game.cards.remove(at: game.cards.index(of: randomCard)!)
cardCount2 -= 1
} while cardCount2 != 0

// nothing changed! cardFiled still have 21 items as after deletion WHY???
print(game.cardField.count)

是否有任何我不知道的值(value)引用细微差别?

最佳答案

您正在从字典中删除随机键,这可能会留下 dic[21] 值,并且您的计数仍然是 21。然后您正在更新索引 21,但计数不会改变。您也可以将数组用于cardFields。

repeat {
let randomCard = game.cards[(game.cards.count-1).random]
game.cardField.append(randomCard)
game.cards.remove(at: game.cards.index(of: randomCard)!)
cardCount -= 1
} while cardCount != 0

// we got 24 items in cardField dictionary
print(game.cardField.count)

// delete 3 random items from cardField

for _ in 1...3 {
let randomKey = game.cardField.count.random
game.cardField.remove(at: randomKey)
}

// we got 21 items in cardField dictionary, ok
print(game.cardField.count)

// let's add 3 another items to cardField dictionary same way
var cardCount2 = 3

repeat {
let randomCard = game.cards[(game.cards.count-1).random]
game.cardField.append(randomCard)
game.cards.remove(at: game.cards.index(of: randomCard)!)
cardCount2 -= 1
} while cardCount2 != 0

// nothing changed! cardFiled still have 21 items as after deletion WHY???
print(game.cardField.count)

关于Swift 字典默默地不存储新项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50616734/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com