Fábio Bernardo

iOS developer. Geek. Occasionally ranting about something.

Read this first

Server-side Swift notes

This is the result of a recent look into server-side Swift for my own personal use. This area is changing everyday, and with less than a month until WWDC, expect most of this to become incorrect in the future.

It all started with me researching the Amazon Alexa Skills SDK: apparently those are just calls to a previously defined Web Service with a predefined list of commands. And since I live outside the US testing Alexa is tricky, so I decided to try my idea with something similar: Slack’s slash commands.

I’ve heard that Swift on the server isn’t for serious projects so this was a perfect fit. With that in mind I followed Ben Snider’s tutorial (thank you) and quickly discovered that this wasn’t exactly the Swift I was used to.

The following is a list of caveats, problems or notes produced during this short project.

Managing Swift versions

The .swift-version file contains the...

Continue reading →

How we work at Mokriya

We can only expect to get better at what we do if we open it up to criticism. And in that spirit of openness I thought I would share a bit about Mokriya’s software development process. Maybe I can inspire some of you to try this at your workplace.

What’s Mokriya

Mokriya is a software development company based in Cupertino, but we have more than 30 employees around the world. The large majority of us work 100% remote.

I’ve been part of Mokriya for 8 months now, as an iOS Developer.

How do we synchronize

Each product has a dedicated team, so usually I only have to sync with three to six people. Finding time for a quick half an hour call every day is easy.

But syncing with the internal team is as important as syncing with the client and for the past months that’s also a daily call for me.

So I have at least two calls every day, 30 minutes each. That single hour saves us multiple...

Continue reading →

Sharing Core Data between App and Extension

For years now I’ve been avoiding Core Data. My first experience with it was back when the iPad first came out and it wasn’t pleasant.

I was able to ignore it for so long because most of the projects I worked on were just client apps for some web service, with no need for local search, indexing or complex relationship graphs. So I was a little worried when I realised one of my projects needed all three.

I since discover my previous problems with Core Data were just part of the learning curve: threading and performance. With GCD Core Data gained support for queues and blocks, making it considerably easy to work with different threads. Performance is something that depends on the specific project but let’s say that knowing how Core Data actually fetches data from the data store helped a lot (RTFM).

For the project at hand I needed the Core Data store, and if possible context, synced...

Continue reading →

Clarity in Swift code

var numbers = [-1, 0, -9, -3, 1, 3, 4]
let first = numbers.first
let (min, max) = numbers.map({($0,$0)}).reduce((first,first), combine: {($0.0 < $1.0 ? $0.0 : $1.0, $0.0 > $1.0 ? $0.0 : $1.0)})

As you can probably guess, this Swift snippet compiles and works, it finds the minimum and the maximum values inside the numbers array.

This code represents my biggest fear regarding Swift. I’m sure many people will actually enjoy this, most won’t care enough to think about it now, but I think this type of code is exactly what makes great languages hard for beginners to grasp.

First, is this is real production code? After all I choose this snippet to make a point. If it compiles someone will ship it, commit it and even share it. Of this I have no doubt.

I come up with this gibberish by “cleaning” up code:

var numbers = [-1, 0, -9, -3, 1, 3, 4]
let first = numbers.first!

var min = first

Continue reading →

NSKeyedUnarchiver and Swift

Too many years using Objective-C (or any another language really) will give you a sixth sense. Let me show you an example: something in the back of your head should shout at the following code.

NSString *string = [self stringOrNil];
NSURL *url = [NSURL URLWithString:string];

If you don’t see the problem don’t give up, you were not bitten enough times. The problem is that +[NSURL URLWithString:] will throw an exception if URLString is nil.
We can avoid this by checking if URLString is nil.

NSString *string = [self stringOrNil];
NSURL *url = string ?[NSURL URLWithString:string] : nil;

Perfect. That’s what I do every project. I must keep in mind that +[NSURL URLWithString:] does that. The compiler won’t force you to implement a try/catch block like in Java, so you must have this present while coding. That sucks.

Now let’s consider the case of NSKeyedUnarchiver:

NSString *filePath =

Continue reading →

WatchKit Day 2

Did I mention that WatchKit was limited?

So far so good, Xcode’s Beta is crashing on an acceptable rate, Swift is starting to make more sense (yes, I’m also starting with that) and I had the opportunity to say my first “no, you can’t do that on the watch”.

I spent a considerable amount of time setting up the project. I’m now using a framework to share code between App and Watch Extension. All in Swift. ?

The rest of the day was spent dealing with tables.

Using WKInterfaceTable and configuring Row Controllers led me to wonder if those controllers were being reused. It makes sense to reuse cells in such a limited device. Probably they are being reused. Does that mean Row Controllers should implement the prepareForReuse: method? No. You won’t find cells on WatchKit, only Row Controllers.

Row Controllers are a concept introduced by Apple. Not a class, not a protocol, a documented...

Continue reading →

My Podcast Subscriptions

Sometimes people ask me what podcasts I subscribe to. This is that list. If we share similar interests some of these require no introduction. If you are now starting with podcasts I suggest you download the last episode of each to see if you like it.

Many of those you can listen at any time. I usually have between a dozen and half a dozen to listen to.

  • Accidental Tech Podcast
  • Core Intuition
  • Debug
  • Developing Prespective
  • The Economist: All Audio
  • Edge Cases
  • Exponent
  • Freakonomics Radio
  • Hello Internet
  • The iDeveloper Podcast
  • Iterate
  • Mokriya
  • Pragmatic
  • The Record
  • Release Notes
  • The Talk Show With John Gruber

In Portuguese

If you don’t speak Portuguese those are useless to you. If you do: experimenta!

  • TSF - Economia Dia-a-Dia
  • TSF - Governo Sombra
  • TSF - Pares da República
  • TSF - Tubo de Ensaio
  • TSF - Tudo é Economia

Continue reading →

WatchKit Day 1

So, WatchKit is here. It supports Glances, Actionable Notifications, and unexpectedly, something Apple calls WatchKit Apps.

I spent my first day reading the programming guide, watching the “getting started” video and playing with Xcode 6.2 Beta. This post is a summary of what I learned. Have in mind that I focused most of my attention on WatchKit Apps and that this is a first Beta. Apple will make changes before the final release.

I must say that Xcode’s Beta surprised me in a good way. It crashed only once for the entire day, and I spent half an hour looking for a way to enable multiple lines in WKInterfaceLabel, only to find out, after restarting Xcode, that the option was right there in Interface Builder.

WatchKit is very limited. What they call an App is just an interface living on the watch that sends user input to the phone, and the phone can send commands to change the...

Continue reading →

Lighter View Controller Data Sources

Inspired by the concept of Lighter View Controllers, neatly explained by Chris Eidhof on objc.io, I came up with a way of separating the view controller, data source and network code.

If you haven’t read the aforementioned article I suggest you do it and then come back to this page.

I started out with an ArrayDataSource like Chris suggested:

self.dataSource = [[ArrayDataSource alloc] initWithItems:photos

tableView.dataSource = self.dataSource;

My experience using ArrayDataSource

After some time three issues popped up:

  1. Most of my view controllers had collection/table views that present results from a web service call. ArrayDataSource only supports arrays, so I had to fetch and parse from the server before creating the data source.
  2. I needed pagination...

Continue reading →