Best practices and how to get your head around reactive programming in Swift

Photo by Sarah Pflug from Burst

Before I start I need to mention that these are a set of best practices that I personally use to get the most out of RxSwift (Rx) and avoid many of the common pitfalls. They are by no means a set of hard rules that will work for everyone, but I encourage you to try them.

Why use Rx?

If so many people struggle with Rx, why use it at all? This is a perfectly valid question.


Photo by Iker Urteaga via Unsplash

Imagine we want to build an app to track books. The API we are building against provides JSON for authors and books, which looks like this:

Swift’s Codable features enable us to quickly create matching models:

Thanks to Codable this is all we have to do to get JSON mapping to type-safe models for free!

This would compile, and although the call-site seems to…


I really like Swifts error handling system for synchronous code. It allows us to produce errors that have all the information we need attached. We are able to choose to handle errors as they occur or throw them further up the chain, delegating that to another part of our code.

Example

Lets have a look…


I’m doing a lot of server side Swift lately — primarily working on my Slackbot Chameleon. I am working on a new feature that requires a HTTP server and I’ve decided to use Vapor. The problem is when trying to debug locally I was unable to expose my macbook builds in a way that is publicly visible to the internet.


Giving Chameleon a Redis brain!

Note: This article/series is deprecated, There has been a lot of changes in the Chameleon framework. I will produce a new set of articles soon.. sorry for not completing this series.

Recap

Last time we went through setting up your first Swift Slack bot and deploying on Heroku. This leaves our bot with only memory based storage. Which means that if our bot ever shuts down we will lose any data we had. Luckily Chameleon comes with support for Redis, we just need to hook everything up!

Installing Redis on Heroku

Heroku provides a Redis add-on with a free tier…


Setting up a Swift Slack bot on Heroku!

Note: This article/series is deprecated, There has been a lot of changes in the Chameleon framework. I will produce a new set of articles soon.. sorry for not completing this series.

What is Chameleon?

Chameleon is an open-source, extensible Slack bot built in Swift. It is capable of running on both macOS and Linux. Instructions for both are on the repository but today I am going to take you through getting setup on Heroku in a bit more detail.

Prerequisites

First, you will need to create a Slack bot user for your team by following this…


Note: Sample project can be found here.

I love MVVM. Ever since adopting it many moons ago the ways in which I architect my apps have vastly improved. I personally credit it to the freedom MVVM allows. It’s a pattern that works hand-in-hand with concepts like “Separation of Concerns” and “Dependency Injection” as well as Functional Reactive Programming.


Complete credit to Logan Wright (Twitter) for the solution to this problem that had me chasing my tail for a few hours today.

Problem

I had a view controller with a custom init that looked something like this:

Nothing fancy here… however this will crash under iOS8 on line 12:

fatal error: unexpectedly found nil while unwrapping an Optional value

I tried all the usual suspects, re-connecting the IBOutlet, full clean, delete derived data, restart xcode… all to no avail.

Cause

The issue here is actually with the behaviour of init(nibName:bundle:). From the docs:

If you specify nil for the nibName


Note: Sample project at the end.

I can be a little OCD when it comes to separation of concerns. Thankfully, for service level objects such as networking or persistence, this is easily achievable. For more complex objects that need other objects to function we have patterns like dependency injection to keep things clean.


A question was asked over on iOS Developers about a ‘swifty’ way of reading a plist into a Dictionary so I decided to do a little research. Currently there are two ways to read in plist data.

NSPropertyListSerialization

This class works with plist data in a manner that’s similar to how NSJSONSerialization works with JSON. You pass it a NSData object and, if successful, it gives you back an AnyObject which you can then try to cast to NSDictionary.

While fairly simple this is the more verbose means…

Ian Keen

Developer, Snowboarder (not at the same time unfortunately) — All posts here are my own

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store