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

时间:2018-05-31 04:37:40

标签: swift dictionary

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

这个结构就像实现了Equatable协议的演示一样。

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)

是否有任何价值 - 参考细微差别,我不知道?

1 个答案:

答案 0 :(得分:0)

您正在从字典中删除随机密钥,这可能会留下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)