简体   繁体   中英

Swift's equatable protocol conformance check

I am trying to extend Swift's Array class with the following func:

func containsObjectIdenticalTo(obj: T) -> Bool {
    // objectPassingTest returns the first object passing the test
    return objectPassingTest { x in x == obj }
}

Apparently, this won't compile as the compiler doesn't know yet if == is implemented for type T . I then change the code to this

func containsObjectIdenticalTo(obj: T) -> Bool {
    return objectPassingTest {
        x in
        assert(x is Equatable && obj is Equatable)
        return (x as Equatable) == (obj as Equatable)
    } != nil
}

Which doesn't work either, since conformance against Equatable can't be checked (because Equatable wasn't defined with @obj ) !

Any thoughts on this? Would be nice if there's some way to assert directly if T conforms to Equatable , but I haven't read that anywhere. Swift seems to be less dynamic than Obj-C in these stuffs.

UPDATE: Tried this suggestion and it doesn't work (don't know exactly what <T: Equatable> is for, tho it does compile).

func containsObjectIdenticalTo<T: Equatable>(obj: T) -> Bool {
    var x = self[0]
    var y = self[0]
    return x == y // Error here
}

指定T必须在Method的签名中相等:

func containsObjectIdenticalTo<T: Equatable>(obj: T) -> Bool {/*...*/}

i got this from ExSwift : https://github.com/pNre/ExSwift

 func contains <T: Equatable> (items: T...) -> Bool {
        return items.all { self.indexOf($0) >= 0 }
    }

func indexOf <U: Equatable> (item: U) -> Int? {
        if item is Element {
            if let found = find(reinterpretCast(self) as Array<U>, item) {
                return found
            }

            return nil
        }

        return nil
    }

func all (call: (Element) -> Bool) -> Bool {
        for item in self {
            if !call(item) {
                return false
            }
        }

        return true
    }

maybe you can try it

How about:

func containsObjectIdenticalTo<T : Equatable>(obj: T) -> Bool {
 ... etc ...
}

In the end, I resorted to this solution

func containsObjectIdenticalTo<U: Equatable>(obj: U) -> Bool {
    return objectPassingTest({
        x in
        return x as U == obj
    }) != nil
}

Might not be the best (ie safest ) one. But it works great.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM