0

I am trying to check whether an array of tuples contains a certain tuple using the native contains() method of Array. I have declared my two "equatable" functions as

public func ==(a: (clip1: Clip?, clip2: Clip?), b: (clip1: Clip?, clip2: Clip?)) -> Bool {

    let clipa1 = a.clip1
    let clipa2 = a.clip2
    let clipb1 = b.clip1
    let clipb2 = b.clip2

    if clipa1 != nil && clipa2 != nil && clipb1 != nil && clipb2 != nil {
        return (clipa1! == clipb1!) && (clipa2! == clipb2!)
    }
    else if clipa1 != nil && clipa2 == nil && clipb1 != nil && clipb2 == nil {
        return (clipa1! == clipb1!)
    } else {
        return false
    }
}

public func ==(a: Clip, b: Clip) -> Bool {
    return a.id == b.id
}

However when I try these in this manner

for clip in tmp {
                if !_filteredClips?.contains((clip1: clip.clip1, clip2: clip.clip2)) {
                    _filteredClips?.append(clip)
                }
            }

I am getting Cannot convert value of type '(clip1: Optional<Clip>, clip2: Optional<Clip>)' to expected argument type '@noescape ((clip1: Clip?, clip2: Clip?)) throws -> Bool'

What am I missing here?

Thanos
  • 844
  • 2
  • 11
  • 27

2 Answers2

0

The version of contains you are trying to use is only available for elements that adopt Equatable:

extension SequenceType where Generator.Element : Equatable {
    /// Return `true` iff `element` is in `self`.
    @warn_unused_result
    public func contains(element: Self.Generator.Element) -> Bool
}

The tuple type (Clip?, Clip?) does not and you can't extend it. Since you've implemented ==, you can use the version of contains that takes a predicate like this.

for clip in tmp {
    if !_filteredClips!.contains({ $0 == clip }) {
        _filteredClips?.append(clip)
    }
}

See How do I check if an array of tuples contains a particular one in Swift? for other ideas.

Community
  • 1
  • 1
Chris Gulley
  • 514
  • 5
  • 7
-1

== can be used to test the equality of custom types such as structures, classes and tuples as the caller is doing here. The choice between == and === depends on whether your code needs to test for equality or identity, not on whether you are working with a class, struct, etc

Gal Marom
  • 8,499
  • 1
  • 17
  • 19
  • Sorry, I am not sure I understand where I should `isEqual`. Care to elaborate a little more please? – Thanos Nov 10 '15 at 17:19
  • I am sorry, this is still unclear. Can you please be more specific on how to modify the code I pasted to avoid the compile time error I am getting? – Thanos Nov 10 '15 at 17:25
  • This answer is misleading. == can be used to test the equality of custom types such as structures, classes and tuples as the caller is doing here. The choice between == and === depends on whether your code needs to test for equality or identity, not on whether you are working with a class, struct, etc. – Chris Gulley Nov 12 '15 at 13:03
  • gotcha.I se why it can be misleading. I've changed the answer – Gal Marom Nov 12 '15 at 14:18