为什么不使用内置
contains()功能?它有两种口味
func contains<S : SequenceType, L : BooleanType>(seq: S, predicate: @noescape (S.Generator.Element) -> L) -> Boolfunc contains<S : SequenceType where S.Generator.Element : Equatable>(seq: S, x: S.Generator.Element) -> Bool
第一个以谓词为参数。
if contains(myArr, { $0.name == "Def" }) { println("yes")}更新:从Swift 2开始, 两个全局
contains()函数都已被 协议扩展方法 取代:
extension SequenceType where Generator.Element : Equatable { func contains(element: Self.Generator.Element) -> Bool}extension SequenceType { func contains(@noescape predicate: (Self.Generator.Element) -> Bool) -> Bool}第一个(基于谓词的)用作:
if myArr.contains( { $0.name == "Def" }) { print("yes")}斯威夫特3:
if myArr.contains(where: { $0.name == "Def" }) { print("yes")}


