如何在swift中观察一组NSObject?

时间:2015-03-07 11:18:04

标签: arrays swift nsobject observable observers

我是快速语言的新手,我的问题是如何在swift中使用observable / observer模式。

我想让我的数组在我的SocketManager类中是可观察的,这样我的UIViewController类就可以观察到它。我使用了Andrew J Wagner编写的Observable类,我从这个链接中得到了:

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

我有阵列:

var marketIndexList:Array< MarketIndex> = []

将从服务器获取其数据。每次从服务器收到新数据时,都会更新此列表。从服务器获取我的Array的值后,我想使它成为Observable类,它由上面的链接实现:

marketIndexList = Observable(marketIndexList)

但我收到了这个错误:

'MarketIndex'与'AnyObject'

不同

MarketIndex是一个类型为NSObject的类,它具有String类型的一些属性。

这是我使用过的Observable类:

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
    }
}

谁能告诉我这是什么问题?

也有人知道在swift中观察一组对象的方法吗?

我将不胜感激。

提前致谢。

1 个答案:

答案 0 :(得分:2)

错误是因为marketIndexList被定义为Array<MarketIndex>,但您分配了Observable个实例。也许你想做这样的事情:

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

// Or maybe
observableList = Observable(marketIndexList)

顺便说一下,你也可以使用Swift的Objective-C KVO。只需将属性标记为dynamic,并确保该类继承NSObject以使属性可观察。例如:

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

除了你提到的内容之外,这篇文章很适合在Swift中阅读KVO。 https://medium.com/proto-venture-technology/the-state-of-kvo-in-swift-aa5cb1e05cba