Bumping from ReactiveCocoa 2.4.7 to ReactiveCocoa 5.0.0

109 Views Asked by At

I have taken over an iOS-project which implements ReactiveCocoa 2.4.7 using CocoaPods. The app was programmed in Swift 1.2, but has later been upgraded, and is now Swift 2.3. However, ReactiveCocoa has not been updated. I am now in a situation where my goal is to upgrade the app to Swift 3, but to do this, I will have to upgrade ReactiveCocoa as well.

In ReactiveCocoa 2.4.7, if I am correct, I am able to observe values that inherit from NSObject, as long as they have the dynamic keyword. From what I understand, this is to make it accessible to RAC which is made in Objective-C. Is there no such thing in 5.0.0? Do I have to change all my variables from

dynamic var name:String? = "MyName"

to

let name:MutableProperty<String?> = MutableProperty("MyName") ?

Is this the correct equivalent in 5.0.0?

1

There are 1 best solutions below

0
MeXx On

ReactiveCocoa 2.4.7 is written in ObjC. If can upgrade the project to Swift 3 without upgrading ReactiveCocoa.

All the changes you'll have to make for ReactiveCocoa 2.4.7 to work in Swift 3 will be due to the change in how Swift 3 imports ObjC Code compared to how Swift 2 did.

See this related question. Converting to Swift 3 renamed my own Objective-C method

Regarding your equivalence question, the answer is Yes and No.

No: In RAC 2.x you could observe any property of a NSObject via the RACObserve macro which works via KVO (in Swift you had to use a wrapper for the macro). For this to work with in Swift, you'll need to use the dynamic keyword for the property to enable KVO.

The equivalent to this in RAC 5.0 is DynamicProperty<Value>. However, as the documentation states:

Use this class only as a last resort! MutableProperty is generally better unless KVC/KVO is required by the API you're using

Therefore:

Yes:, MutableProperty most of the time is the correct equivalent. But it requires you to make more changes and will not work the same way as RACObserve did.

With RACObserve or DynamicProperty, you would just set person.name = "Rudolph", and Observers of the name property would be called.

With MutableProperty, you will have to set person.name.value = "Rudolph".