簡體   English   中英

如何解決“參數類型'CustomStruct'與預期類型'Sequence'不符”

[英]How to solve “Argument type 'CustomStruct' does not conform to expected type 'Sequence'”

我嘗試使用下面的代碼洗牌陣列由稱為簡單的自定義結構的Card ,我在得到一個錯誤cards.remove(at: randomIndex)

Error: Argument type 'Card' does not conform to expected type 'Sequence'

以下是代碼:

var cards = [Card]()  // declare the array
var shuffledCards = [Card]()
for _ in cards.indices {
    let randomIndex = Int(arc4random_uniform(UInt32(cards.count)))
    shuffledCards += cards.remove(at: randomIndex)  // error appears here
}
cards = shuffledCards

奇怪的是,與此相反,類似的設計適用於Array<String>

var emojiChoices = ["🦇", "😱", "🙀", "😈", "🎃", "👻", "🍭", "🍬", "🍎"]
let randomIndex = Int(arc4random_uniform(UInt32(emojiChoices.count)))
emoji[card.identifier] = emojiChoices.remove(at: randomIndex)

我應該添加Card的定義嗎? 如果是這樣,我應該添加什么? 謝謝!

中的+=運算符

shuffledCards += cards.remove(at: randomIndex)

期望將元素序列附加到shuffledCards數組(例如另一個數組)上。 要附加單個元素,請使用

shuffledCards.append(cards.remove(at: randomIndex))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM