Sunday, January 17, 2016

Swift Golf

Cocoanetics:

We learned several new ways to pick out a specific element from a collection. I found the discussion exhilarating and uniquely instructive. In particular combining for + where and for + case let were new to me. I am sure that I will soon find more uses for these.

Playing Code Golf in Swift is not about brevity, but mostly about beauty, elegance and code readability. Because of this I ended up with an approach that reads like plain english to me: “from the cell’s content view’s subviews, get the first element of type UISwitch”. Anybody can understand that.

I would lean towards a combination of Variant 6 and Variant 3. It probably doesn’t matter much for this particular case, but a reusable generic extension should not flatMap the entire collection just to find one element.

Update (2016-01-20): Arkadiusz Holko:

V. 6 can be even smarter by inferring generic from return type.

This is cool but seems risky and potentially confusing to me.

Comments RSS · Twitter

Leave a Comment