Aleksandar • Vacić

iOS bits and pieces

Swift one-line type-checking lasting 27s

In the Swift project I finished recently there was a very strange behavior. At some point I noticed that autocomplete totally stopped working and the indexer was constantly spinning. Only if I would wait for what felt like minutes, things would settled down and it was working fine for a while. Only to re-appear again after I add few more types or methods.

It’s rather large project with about 60k lines of code, but still was really strange. After I got fed up, I began hunting for solution. Luckily for me, rather quickly I found Soroush’s post describing these unofficial Other Swift Flags:

1
2
-Xfrontend
-warn-long-function-bodies=100

This is where you place them, in target’s Build Settings

This warns you when any of your methods take over 100ms to finish type-checking. In my project, I found few nasty surprises:

27s to type-check a single method?!

This last method, taking up 27s to type-check, had a total of 5 lines and the problem turned out to be string concatenation. When I replaced it with string interpolation, it dropped below 50ms.

This was really insane and I was curious why would this one line cause so much work for Swift compiler. Matt Gallagher of Cocoa with Love encountered this few months before me, found the issue and even offered a solution.

update: Jordan Rose chimed in on Twitter about the proposed solution:

MVC-C: Injecting Coordinator pattern in UIKit

I first learned about Coordinators from Soroush Khanlou’s talk at NSSpain 2015 and two related articles on his blog.

A bit later, Krzysztof Zabłocki wrote about more or less identical concept he calls Flow Controllers.

I got intrigued because it was the first iOS app architecture approach I’ve seen that (from my point of view) didn’t try to replace nor denigrate MVC — it embraced and extended it. It builds on existing design patterns: Model View Controller (MVC), Delegation and Dependency Injection (DI), all already proven to work well in practice.

In this article I want to present my evolution of the Coordinator approach that helped me conquer rather large app with 10 or so distinct parts and 100+ UIViewControllers in total.

In-app language change in iOS app

iOS – and Apple OSs in general – have excellent I18N support. You more or less don’t have to worry about date and number formatting as long as you are following the rules. The essential rules are:

  • all translations are prepared beforehand and compiled into the app
  • language choice is done outside your app, in system' Settings

Fairly often, I have requests from clients to implement in-app language change, which should (at least) instantly translate the app. There is no API support in iOS frameworks for this.

It’s not impossible though.

Masterclass: Core Data tidbits

I have been using Core Data for a while now, several years at least. Over time, I have learned how it works internally and learned to side-step possible issues and shortcomings. The following is a unordered list of tidbits.