Skip to content

Instantly share code, notes, and snippets.

@batitous
Created June 30, 2016 09:47
Show Gist options
  • Save batitous/e6d6ca5889f63b55d892a4dce16a720d to your computer and use it in GitHub Desktop.
Save batitous/e6d6ca5889f63b55d892a4dce16a720d to your computer and use it in GitHub Desktop.
Compare 2 optionnel arrays in Swift, thanks to Sysy !
// At present, Optional<Array<T>> is not possible to make equatable even though T might be.
// This fakes that that behaviour to make comparisons between optional arrays simpler.
func !=<C: Equatable>(lhs: [C]?, rhs: [C]?) -> Bool {
return !(lhs == rhs)
}
func ==<C: Equatable>(lhs: [C]?, rhs: [C]?) -> Bool {
switch (lhs, rhs) {
case (.Some(let lhs), .Some(let rhs)):
return lhs == rhs
case (.None, .None):
return true
default:
return false
}
}
@batitous
Copy link
Author

let arr: [Int]? = nil
let arr2: [Int] = [1]
let equal = arr == arr2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment