数组 – 如何在swift中观察一组NSObject?

前端之家收集整理的这篇文章主要介绍了数组 – 如何在swift中观察一组NSObject?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是快速语言的新手,我的问题是如何在 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中观察一系列对象的方法吗?

我将不胜感激任何帮助.

提前致谢.

错误是因为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.只需将属性标记为动态,并确保该类继承NSObject以使属性可观察.例如:

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

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

原文链接:https://www.f2er.com/swift/319410.html

猜你在找的Swift相关文章