ReactiveCocoa 4.0 & Swift 2.0

This article is a follow-up for my previous piece about how to update your app with the new ReactiveCocoa 3.0 Swift API. It has been released a few weeks ago, but Swift 2 already happened since, so the time has come to step a major version forward. Here we go, 4.0 alpha 2!

I did the same with BrewMobile, the pet project I'm trying out new stuff with, let's see how things look like now with Swift 2 & ReactiveCocoa 4.0.

Setup

There are a few things to setup in advance.

Carthage

Let's check out the Cartfile first:

github "ReactiveCocoa/ReactiveCocoa" "v4.0.0-alpha.2"

if you experience errors during your carthage update just clean your DerivedData and shoot:

carthage update --platform iOS --no-use-binaries

Making sure it won't use cached versions from the pre-built frameworks like RAC itself.

Xcode 7

Select Edit>Convert>To The Latest Swift Syntax and let it do it's magic, it helps to avoid some boring refactoring minutes.

ReactiveCocoa 4.0

It's time to update the code using ReactiveCocoa to the latest & greatest. I suggest to read the change log first, that's always up-to-date.

. is the new |>

Since they turned Signal and SignalProducer operators into protocol extensions (which is really cool), it became possible to drop the global function setup and use dot notation for chaining the operators. Hence I started with replacing the pipe (|>) operators with the good old dot notation syntax.

catch going flatMapError

Another change was to replace catch with flatMapError everywhere, since catch became reserved with in Swift 2.

.value instead of put()

I encountered several errors about the missing put function on the MutableProperty interface. Now it actually has a value wrapped which can be directly set to send a next to its observers. So

became

start, observe

Although these are indicated to likely being changed again in later versions, it is important to check out the changes of start and observe.

Now

looks like:

I hope it helped some, but if you experience problems with ReactiveCocoa when you're upgrading, I suggest to open an issue. The contributors seems to be really helpful.

Swift 2.0

With the Swift 2 parts, I faced several issues after the automatic Xcode conversion, besides some minor improvements like count as a property on CollectionType.

Error handling

With the original error handling approaches gone, error arguments became extra in the new Swift 2 compiler. We have try/catch now!

became:

There's a lot more to the new error handling. Check out @ericasadun's blog for articles like asynchronous error handling in Swift 2 and such.

iOS 9

Xcode will kindly ask you to replace HTTP requests with a secure one (it won't work otherwise). Although it is possible to whitelist insecure URLs in the app's Info.plist.

About & more

Please note, these changes were the necessary ones, there is so much more you can improve in your code with Swift 2.0 and ReactiveCocoa 4.0 as well. If you find something in BrewMobile you would definitely make better, just add a comment below (or a PR on GitHub).

To stay up-to-date with Swift, it's good to read Weekly Swift resources, NSHipster, Swift A Day, or subscribe to the Swift Sandbox newsletter.

Show Comments