How to observe an array of NSObjects in swift?

I am new to fast language and my problem is how to use observable / observational pattern quickly.

I want to make my array visible in my SocketManager class so that it can be observed by my UIViewController class. I used the Observable class written by Andrew J Wagner, which I got from this link:

http://www.drewag.me/posts/swift-kvo-substitute-observable-variables

I have an array:

var marketIndexList: Array <MarketIndex> = []

which will receive its data from the server. This list will be updated every time new data is received from the server. After I got the values โ€‹โ€‹of my array from the server, I want to make it a class of type Observable, which is implemented using the link above:

marketIndexList = Observable (marketIndexList)

But I got this error:

"MarketIndex" is not identical to "AnyObject"

MarketIndex is a class of type NSObject that has some properties of type String.

This is the Observable class that I used:

import Foundation class Observable { typealias DidChangeHandler = (oldValue: Array<MarketIndex>?, newValue: Array<MarketIndex>) -> () var value : Array<MarketIndex> = [] { didSet { for (owner, handlers) in self.observers { for handler in handlers { handler(oldValue: oldValue, newValue: value) } } } } init(_ value: Array<MarketIndex>) { self.value = value } func addObserverForOwner(owner: IndexViewController, triggerImmediately: Bool, handler: DidChangeHandler) { if let index = self.indexOfOwner(owner) { // since the owner exists, add the handler to the existing array self.observers[index].handlers.append(handler) } else { // since the owner does not already exist, add a new tuple with the // owner and an array with the handler self.observers.append(owner: owner, handlers: [handler]) } if (triggerImmediately) { // Trigger the handler immediately since it was requested handler(oldValue: nil, newValue: self.value) } } func removeObserversForOwner(owner: AnyObject) { if let index = self.indexOfOwner(owner) { self.observers.removeAtIndex(index) } } // #pragma mark - Private Properties var observers: [(owner: IndexViewController, handlers: [DidChangeHandler])] = [] // #pragma mark - Private Methods func indexOfOwner(owner: AnyObject) -> Int? { var index : Int = 0 for (possibleOwner, handlers) in self.observers { if possibleOwner === owner { return index } index++ } return nil } } 

Can someone tell me what the problem is?

Also does anyone know a way to observe an array of objects in swift?

I would be grateful for any help.

Thanks in advance.

+8
arrays swift observable nsobject observers
source share
1 answer

The error is that marketIndexList is defined as Array<MarketIndex> , but you assigned an instance of Observable . Perhaps you wanted to do something like this:

 var observableList: Observable = Observable([]) var marketIndexList: Array<MarketIndex> = [MarketIndex(), MarketIndex()] observableList.value = marketIndexList // Or maybe observableList = Observable(marketIndexList) 

By the way, you can also use Swift's Objective-C KVO. Just mark the property as dynamic and make sure the class inherits NSObject to make the property observable. For example:

 class ObservableClass: NSObject { dynamic var value = [Int]() } 

This post is useful to read for KVO in Swift in addition to what you were talking about. https://medium.com/proto-venture-technology/the-state-of-kvo-in-swift-aa5cb1e05cba

+2
source share

All Articles