我试图检查一个数组数组是否包含一个字符串数组。我的错误消息说:
"无法找到'包含'接受类型'([([(String)])]),[(String)])'"
var allCards = [[String]]()
var exp1 = [String]()
if !contains(allcards, exp1) {
allCards.append(exp1)
}
发生了什么事?!
答案 0 :(得分:7)
Array
不符合Equatable
协议,因此
/// Return `true` iff `x` is in `seq`.
func contains<S : SequenceType where S.Generator.Element : Equatable>(seq: S, x: S.Generator.Element) -> Bool
不能在这里使用。您可以使用基于谓词的变体
/// Return `true` iff an element in `seq` satisfies `predicate`.
func contains<S : SequenceType, L : BooleanType>(seq: S, predicate: @noescape (S.Generator.Element) -> L) -> Bool
代替:
var allCards = [[String]]()
var exp1 = [String]()
if !contains(allCards, { $0 == exp1 } ) {
allCards.append(exp1)
}
因为为==
元素的数组定义了Equatable
。
答案 1 :(得分:1)
更新 String[] easyWords= new String[]{"integer","project","octopus"}; //+1000s more words
String easyrnd = (easywords[new Random().nextInt(easywords.length)]);
String letterguess = consolereader.nextLine();
for(int i=0;i<easyWords.length;i++){
if(letterguess==easywords[i]){
// do something
}
:
Swift2
现在是contains()
协议扩展中定义的函数,因此:
Sequence