All posts by Zoli

About Zoli

iOS developer at Halcyonmobile.

Article – option sets in Swift

I found a great article on option sets in Swift. The article is signed by Ole Begemann. I’m not entirely sure if I like them or not. I’m working on a hybrid project, combining Swift with Objective C. So my Swift structs aren’t available in ObjC. I’m afraid of code duplication. Here’s the entire article.

iOS app performance

Excellent article on app performance and what you can do about it:

Measuring the startup time is really easy and can have a great impact on the user experience. I wouldn’t release an app with an extra thread to detect responsiveness issues on the main thread, but it is definitely a good idea in testing environment. It can point out some issues at an early stage. Collecting performance metrics from your users also seems a good idea to me.

Note: I didn’t mention anything about Instruments, because it is obvious you should use it regularly.

Unidirectional data flow in practice

Recently I made a small test project, where I could try out the unidirectional data flow in swift, suggested by Benjamin Encz. You can watch his talk on Realm: What I was interested in, is how can I integrate it into a complex project where I have to perform async tasks. I found out that there is no support for async tasks yet, but you can still achieve it with a workaround. However I would be glad to see it integrated.

The workaround is that in your controller you don’t start actions directly. You implement a service layer instead, and you perform async tasks through that (actually I prefer performing every tasks through the service layer). When the task is done, you create the action inside the service, and send it to the store. So for example if you have to fetch some data from the server, you call the API from the service, and when you get the data you create the corresponding action. The data process part (JSON parsing) can happen either inside the service or the reducer. I’m not sure which option is the better approach.

The only downside is that you have to make sure you return to the main thread. Anyway the profit you gain using this library exceeds this little inconvenience: you have the current state in flows everywhere in the app, and you don’t have to pass it from one controller to another manually. Also you have fresh data everywhere in the app.

Swift: flatMap in action

I am working on an investment related project now. It has plenty of features, I’m not gonna go through them. One of the tasks I had was to show the tickers the user owns in a comma separated list. The problem was the data I had available didn’t contain the necessary field. So I had to iterate through a list of stock objects, and call the appropriate service method to get the ticker for every element. My code looked something like this:

It is working okay, but it is ugly, and hard to understand what it is going on at the first glance. So I started experimenting a bit with flatMap. It turned out to be pretty cool. You can do whatever you like in the closure to every single element of the array, and a new array is returned. Also ensures that there are no nil values in the result. So I ended up with this:

At first it may look strange, but actually it is easy to read, easy to understand.

Swift is awesome!

Functional Programming in Swift

I finally finished reading the book Functional Programming in Swift. I have to say it is a pretty good book. I wouldn’t recommend it for a beginner in Swift, but after you familiarize yourself a bit with the language, it is definitely worth reading.

After this I am seriously considering learning Haskell. I think it would make my functional programming skill much better.