RACObserve 不提供值。 (反应 Cocoa 与 Swift)

RACObserve doesn't give a value. (Reactive Cocoa with Swift)

dynamic var categories: [Category]?
dynamic var productViewModels: [ProductViewModel]?

var totalCategories = 1

dynamic var currentPage = 0

override init() {
    super.init()

    RACObserve(self, "categories").subscribeNext { [weak self] (x) in
        if let this = self, let categories = x as? [Category] {
            this.totalCategories = categories.count ?? 1 // Why this code doesn't work?
            this.productViewModels = categories.map { ProductViewModel(category: [=12=].tagName) }
            this.currentPage = 0
        }
    }

    categories = Category.temporaryInitializer()
}

我想如果 x 有一些价值,RACObserve(self, "categories") 将与包含 categories 的价值一起工作。但它不包含任何价值。 我不知道为什么 x 总是 nil.

====== 问题解决了 ======

categories = Category.temporaryInitializer() 不应该在 init().

我想 RACObserveinit() 之后工作正常。

但我还是不知道为什么。

根据Apple Documentation

When you assign a default value to a stored property, or set its initial value within an initializer, the value of that property is set directly, without calling any property observers.

Reactive observers 在 KVO 上工作。我想这就是为什么您的观察员没有被调用的原因。