gpt4 book ai didi

arrays - 类型 'String?' 不符合协议(protocol) 'Equatable'

转载 作者:行者123 更新时间:2023-11-30 13:57:43 24 4
gpt4 key购买 nike

在我下面的代码中,winRankArray 的内容都是字符串对象。我遇到的问题是在最后设置 didWinRank 变量会出现以下错误:

Type 'String?' does not conform to protocol 'Equatable'

我尝试删除,以便设置卡牌等级看起来像var cardRank1 = DeckDictionary[cardKey1].first,但这给了我一个错误,指出

Value of optional type 'Array?' not unwrapped; did you mean to use '!' or '?'?

如何设置变量以使生成的 winRankArray 不会出现此问题?

        var cardKey1 = "card\(firstRandomNumber)"
var cardRank1 = deckDictionary[cardKey1]?.first

var cardKey2 = "card\(firstRandomNumber)"
var cardRank2 = deckDictionary[cardKey2]?.first

var cardKey3 = "card\(firstRandomNumber)"
var cardRank3 = deckDictionary[cardKey3]?.first

var cardKey4 = "card\(firstRandomNumber)"
var cardRank4 = deckDictionary[cardKey4]?.first

var cardKey5 = "card\(firstRandomNumber)"
var cardRank5 = deckDictionary[cardKey5]?.first

var cardKey6 = "card\(firstRandomNumber)"
var cardRank6 = deckDictionary[cardKey6]?.first

var cardKey7 = "card\(firstRandomNumber)"
var cardRank7 = deckDictionary[cardKey7]?.first

var winRankArray = [cardRank1, cardRank2, cardRank3, cardRank4, cardRank5, cardRank6, cardRank7]

//func sameRank {loop through winRankArray, find 3 identical values, set winRankStatus to true}
let didWinRank = winRankArray.slidingWindowWithLength(4).contains{ $0.allEqual() }

最佳答案

Type 'String?' does not conform to protocol 'Equatable'

我不这么认为...

var s1: String? = "alfa"
var s2: String? = nil
var s3: String? = "beta"
var arr: Array<String?> = [s1, s2, s3]
arr.contains { (str) -> Bool in // true
str == "alfa"
}
s1 == "alfa" // true
s1 == s3 // false
s2 == nil // true

When adopting Equatable, only the == operator is required to be implemented. The standard library provides an implementation for !=.

关于arrays - 类型 'String?' 不符合协议(protocol) 'Equatable',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33405887/

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