Even though they are sometimes overused, enums are still a great tool. Thanks to Codable they can easily be decoded from JSON sent by your backend. This is great until you need to add a new case to the enum. Then you have to either deal with versioning your API or you need to tell …
Category Archives: Swift
Enum or struct?
I often see a pattern where there is an enum with a bunch of computed properties that return a different value for each case. In the rest of the code base then only those properties are used. Something like this: This works, but quite often can be improved by using a struct instead of an …
Automatic builders
While investigating the new AttributedString API I realized you could use the same techniques to build a generic fluid builder interface for any type. This is what I came up with: And here is an example how you can use it:
New attributed strings
Finally we are going to get attributed strings as a native Swift value type. So lets see how we can update the example of my last article about attributed strings: Pretty neat. No more NSRange to deal with, and we can access the font attribute directly as a typed property. No more dealing with attribute …
Attributed Strings
I had to take a string like “Name (whatever)” and display it in a label. The Name part should be bold, and anything in parentheses should use the regular font. Pretty simple, I just put the string in a NSAttributedString, find the opening parenthesis and add a bold font attribute to everything up to it. …
Factories and Protocol Composition DI
Especially on iOS it’s often necessary to create new objects like new view controllers or such things. This often cannot be avoided. This usually is solved by having the object that creates new objects also require all the dependencies of the objects it wants to create. This of course is not a great thing as …
Protocol Composition and Dependency Injection
I recently read the article “Using protocol compositon for dependency injection” by Krzysztof Zabłocki. I found this to be a rather interesting technique for doing DI, but when trying to implement it I found it somewhat limited: It doesn’t really handle dependencies that have dependencies themselves. No support for lazy instantiation, everything needs to be instantiated up front. …
Continue reading “Protocol Composition and Dependency Injection”