问题描述
我正在尝试测试类型 [Any]
的数组是否包含特定类型的值(例如, Int
)。
I'm trying to test if an array of type [Any]
contains a value of a specific type (say, Int
).
我知道Swift不知道如何比较 Int
和值的类型,我想这就是自动完成模板中要表达的内容:
I'm aware that Swift does not know how to compare an Int
and a value of arbitrary type, and I guess that's what's being expressed in the autocomplete template:
包含(谓词:(protocol<>))抛出- >布尔)
所以我尝试了以下代码:
So I tried this code:
let intValue:Int = 5 // for example
let isContained = myArrayOfAny.contains({ element in
return ((element as? Int) == intValue)
})
...并且可以编译(无法确定是否可以正常使用) );但仍然不能成为谓词((协议))
部分的首尾。这是什么意思? SequenceType
的文档非常隐秘:
...and it compiles (can't make sure it works yet); but still can't make heads or tails of the predicate: (protocol<>)
part. What does it mean? The documentation for SequenceType
is quite cryptic:
contains(_: (Self.Generator.Element) throws -> Bool) rethrows -> Bool
Default Implementation
Return true iff an element in self satisfies predicate.
Declaration
@warn_unused_result func contains(@noescape _ predicate: (Self.Generator.Element) throws -> Bool) rethrows -> Bool
编辑:我很困惑,因为我看到的所有搜索结果都具有 Array
的 contains()
方法取要测试是否包含的值,例如:
I'm quite confused because, all search results I've seen have the contains()
method of Array
as just taking the value to be tested for containment, e.g.:
if myArrayOfInt.contains(someInt){
...
而不是闭包。
推荐答案
有两种不同的 contains()
方法(这两种协议的
扩展都是 SequenceType
)。第一个是
There are two different contains()
methods (both protocolextensions to SequenceType
). The first one is
extension SequenceType where Generator.Element : Equatable {
/// Return `true` iff `element` is in `self`.
@warn_unused_result
public func contains(element: Self.Generator.Element) -> Bool
}
,它要求序列元素符合平等的
协议,该协议保证可以将它们与 ==
进行比较:
and it requires that the sequence elements conform to the Equatable
protocol, which guarantees that they can be compared with ==
:
public protocol Equatable {
// ...
public func ==(lhs: Self, rhs: Self) -> Bool
}
例如,在
let intValue:Int = 5
let myArrayOfInt = [4, 5, 6]
let isContained = myArrayOfInt.contains(intValue)
您有一个 Int
和<$ c $的数组c> Int 符合 Equatable
,
,因此此 contains()
方法可以用来检查某个元素的存在
。
you have an array of Int
, and Int
conforms to Equatable
,so this contains()
method can be used to check for the presenceof a certain element.
这不适用于
let myArrayOfAny : [Any] = [4, 5, 6]
因为任何
不符合 Equatable
。在这里,您可以使用
作为第二个 contains()
方法
because Any
does not conform to Equatable
. Here you can usethe second contains()
method
extension SequenceType {
/// Return `true` iff an element in `self` satisfies `predicate`.
@warn_unused_result
public func contains(@noescape predicate: (Self.Generator.Element) throws -> Bool) rethrows -> Bool
}
需要一个谓词,即布尔值功能。将谓词应用于每个数组元素,而不是将
与 ==
进行比较。
此方法不需要数组元素 Equatable
。
which takes a predicate, i.e. a boolean function. Instead of comparing with ==
, the predicate is applied to each array element.This method does not require that the array elements are Equatable
.
这就是您要做的
let myArrayOfAny : [Any] = [4, 5, 6]
let intValue:Int = 5 // for example
let isContained = myArrayOfAny.contains({ element in
return ((element as? Int) == intValue)
})
如果可以将任何数组元素转换为 Int,这将产生
true
并等于给定的 intValue
。
This will yield true
if any array element can be cast to an Int
and is equal to the given intValue
.
可能会看到第一种方法使用频率更高,但是第二种
则更为通用,例如,如果myArrayOfCustomObjects.contains({$ 0。$
The might see the first method more frequently but the secondis far more general, e.g.
if myArrayOfCustomObjects.contains ({ $0.someProperty == "foo" })
或
if myArrayOfInts.contains ({ $0 > 17 })
因此,第一个 contains()
是第二种的一种专门化形式,用于对平等的元素。它可以 实现为
So the first contains()
is a kind of specialization of the second one for a simple containment check in arrays of equatable elements. It could be implemented as
extension SequenceType where Generator.Element : Equatable {
public func contains(element: Self.Generator.Element) -> Bool {
return self.contains({ $0 == element } )
}
}
您会发现与 indexOf()
相同,它也有
两种风格:
You'll find the same with indexOf()
which also comes intwo flavors:
extension CollectionType where Generator.Element : Equatable {
// ...
public func indexOf(element: Self.Generator.Element) -> Self.Index?
}
extension CollectionType {
// ...
public func indexOf(@noescape predicate: (Self.Generator.Element) throws -> Bool) rethrows -> Self.Index?
}
这篇关于[Any]和contains()的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!