0

I am creating a class in swift work as a container of a set of protocols. Below is the source code. The KeyValueObserverDelegate protocol is added in KeyValueObserverService class by addObserver() method. The problem happens on removeObserver() method, the line is index = array.indexOf($0 == observer).

I got an error:

anonymous closure argument not contained in a closure.

I don't know what wrong with my class. How can I get the index of an object from an array?

class KeyValueObserverService{

    private var observerList:Dictionary<String, [KeyValueObserverDelegate]> = Dictionary()

    func addObserver(key:String, observer:KeyValueObserverDelegate){
        var array:Array<KeyValueObserverDelegate>?
        if observerList.keys.contains(key){
            array = observerList[key]
        } else {
            array = Array<KeyValueObserverDelegate>()
            self.observerList[key] = array
        }
        array?.append(observer)
    }

    func updateValueForKey(key:String, value:AnyObject?){
        let array = self.observerList[key];
        if array == nil{
            return
        }
        for  element in array!{
            element.valueChanged(value)
        }
    }

    func removeObserver(key:String, observer:KeyValueObserverDelegate){
        if self.observerList.keys.contains(key) == false{
            return
        }
        var array:[KeyValueObserverDelegate] = self.observerList[key]!;

        let index:Int?


        index = array.indexOf($0 == observer)

        array.removeAtIndex(index!)
    }
}

protocol KeyValueObserverDelegate :class{
    func valueChanged(value:AnyObject?)
}

1 Answer 1

1

Read the error message carefully

...not contained in a closure

By definition a closure is enclosed in a pair of curly braces

array.indexOf({$0 == observer})

or with trailing closure syntax

array.indexOf{$0 == observer}

Edit:

Since a protocol does not conform to Equatable by default use the identity operator

array.indexOf{$0 === observer}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for correcting me. After make this change, the error message becomes "Binary operator "==" cannot be applied to "KeyValueObserverDelegate" operands.
KeyValueObserverDelegate seems to be a custom type. You need to make the type conform to the Equatable protocol by implementing the == operator.
KeyValueObserverDelegate is defined as a protocol, it is located at the bottom of my post. How can I conform to Equatable?
KeyValueObserverDelegate is defined as a protocol, it is located at the bottom of my post. When I add Equable, my code will report a lot errors. Most of them are "KeyValueObserverDelegate can only be used as a generic constraint"
Either use the identity operator (===) or add an identifier property or something else unique to the protocol to be able to identify the particular observer. I edited the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.