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) {
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.
arrays swift observable nsobject observers
user2366997
source share