Cannot convert value of type Option to expected type of argument @noescape (Option) throws & # 8594; Bool

I have a class called Option. This is a Realm object, so it is a subclass of the Realm native base class Object.

In the view controller, I have an array property that contains a bunch of objects Option.

private var options = [Option]()

In the view controller's down method, I need to check if a specific object is contained in the above parameter array Option.

Earlier (Swift 1.2) I have validation logic like this.

func itemSelected(selection: Object) {
    let option = selection as! Option

    if !contains(options, option) {
        // act accordingly
    }
}

Now I am converting the project to Swift 2 (I also upgraded the version of Realm to the version of Swift 2). I updated the code before that.

func itemSelected(selection: Object) {
    let option = selection as! Option

    if !options.contains(option) {
        // act accordingly
    }
}

But now I get the following compilation error!

'Option' '@noescape (Option) throws → Bool'

, . ?

+4
2

, contains , . , ,

if !(options.contains{$0==option}) {
    // act accordingly
}

, , true, . $0 , contains, true, , .

+9

, , contains Equatable, , . Realm Object NSObject, Equatable ( , ). Realm GitHub: https://github.com/realm/realm-cocoa/issues/2519. Realm , , Swift.

, Equatable Hashable, ( bdash GitHub ):

public class A: Object, Equatable, Hashable {
}

public func ==(lhs: A, rhs: A) -> Bool {
    return lhs.isEqual(rhs)
}

A Option .

, XCode 7.2.1, Swift 2.1.1.

+1

Source: https://habr.com/ru/post/1615089/


All Articles