I've found the .contains(Element) method pretty essential in my minimal experience writing Swift code, and have quickly realized that Apple changed it...
func contains(check:[[[Int]]], forElement: [[Int]]) -> Bool {
for element in check {
if areEqual(element, forElement) {
return true
}
}
return false
}
func areEqual(_ a:[[Int]], _ b:[[Int]]) -> Bool {
for i in 0..<a.count {
if a[i] != b[i] {
return false
}
}
return true
}
I've been messing with some large arrays, so I fixed my problem with that clunky function.
What happened?
How do you use the new way?
The example there is well over my head.
enum HTTPResponse {
case ok
case error(Int)
}
let lastThreeResponses: [HTTPResponse] = [.ok, .ok, .error(404)]
let hadError = lastThreeResponses.contains { element in
if case .error = element {
return true
} else {
return false
}
}
// 'hadError' == true
containson? Can you show some code of how you're trying to usecontains?contains(where:is available in Swift 3, too.contains(_:)orcontains(where:)in Swift 4 – could you expand on what you think has changed?[[Int]]is notEquatablebecause we don't have conditional conformances yet – so you should usecontains(where:)with an overload of==for nested arrays, compare stackoverflow.com/q/33377761/2976878 for how to define that.