简体   繁体   English

ReactiveCocoa可以与纯c ++ ViewModel一起使用吗?

[英]Can ReactiveCocoa be used with pure c++ ViewModels?

The motivation is the sharing of view models across platforms (Android, Windows Phone). 动机是跨平台(Android,Windows Phone)共享视图模型。

For example, consider: 例如,考虑:

RAC(self.nameField,text) = RACObserve(self.viewModel, playerName);

What if self.viewModel is pure C++(11)? 如果self.viewModel是纯C ++(11)怎么办?

Obviously C++ doesn't have properties, but perhaps it can be made to work somehow? 显然,C ++没有属性,但是也许可以使其以某种方式工作? Or is ReactiveCocoa coupled to Objective-C on both sides (naturally being coupled on the UI side is no big deal, as some other binding mechanism would have to be used for the relevant environment regardless). 还是ReactiveCocoa在两侧都与Objective-C耦合(自然在UI侧耦合并不重要,因为无论如何,相关环境都必须使用其他绑定机制)。

Obviously C++ doesn't have properties, but perhaps it can be made to work somehow? 显然,C ++没有属性,但是也许可以使其以某种方式工作?

Wrap your C++ objects in Objective-C wrappers. 用Objective-C包装器包装C ++对象。

Or is ReactiveCocoa coupled to Objective-C on both sides (naturally being coupled on the UI side is no big deal, as some other binding mechanism would have to be used for the relevant environment regardless). 还是ReactiveCocoa在两侧都与Objective-C耦合(自然在UI侧耦合并不重要,因为无论如何,相关环境都必须使用其他绑定机制)。

There are some UI-specific extensions to UIKit and AppKit in ReactiveCocoa, but the core of the library has no dependency on or knowledge of "the UI side" at all, but most of the functionality in ReactiveCocoa 2.x relies on runtime features in Objective-C objects. ReactiveCocoa中对UIKit和AppKit有一些特定于UI的扩展,但是该库的核心完全不依赖“ UI端”,也不知道,但ReactiveCocoa 2.x中的大多数功能都依赖于运行时功能。 Objective-C对象。

RACObserve() is just a macro for a method that ultimately ends up calling -[NSObject rac_observeKeyPath:options:observer:block:] , which itself uses an Objective-C-specific technology called Key Value Observing . RACObserve()只是方法的宏,该方法最终最终调用-[NSObject rac_observeKeyPath:options:observer:block:] ,该方法本身使用一种特定于Objective-C的技术,称为键值观察 C++ objects don't support KVO out of the box, so they don't work out of the box with RACObserve() . C ++对象不支持开箱即用的KVO,因此它们无法通过RACObserve()开箱即用。

ReactiveCocoa relies on a Cocoa style implementation of the observer pattern, which in turn relies on the dynamic-dispatch nature of Objective-C. ReactiveCocoa依赖于观察者模式的Cocoa风格实现,而后者又依赖于Objective-C的动态调度特性。

In Cocoa observers work by performing isa-swizzling of a class (reassigning the class pointer to a runtime generated subclass. The sub-class overrides the setter method to notify an associated reference of observers when a property changes. 在Cocoa中,观察者通过对类执行isa-swizzling(将类指针重新分配给运行时生成的子类来进行工作。该子类重写setter方法,以在属性更改时通知观察者的关联引用。

As C++ uses static/vtable style of dispatch, this style of method interception isn't supported. 由于C ++使用静态/ vtable调度方式,因此不支持这种方法拦截。 Therefore ReactiveCocoa observers won't work. 因此,ReactiveCocoa观察者将无法使用。 So the answer is essentially no, ReactiveCocoa won't work for pure C++ classes, although the underlying principles could be applied, or you might look for an Rx library for C++. 因此,答案实际上是“否”,尽管可以应用基本原理,或者您可能会寻找C ++的Rx库,但是ReactiveCocoa不适用于纯C ++类。

Interestingly, Swift defaults also to static/vtable dispatch (though supports dynamic dispatch if you extend NSObject or use the @objc decoration). 有趣的是,Swift默认还使用静态/ vtable调度(尽管如果扩展NSObject或使用@objc装饰,则支持动态调度)。 Here's a n article describing another style of observer for Swift . 这是一篇介绍Swift观察者另一种风格的文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM