Rx with Objective-C using NSInvocation

I want to make a point that NSInvocation is a really powerful tool. If we do not care too much about a compiler type checking every single line of our code we can build a scalable Rx implementation using NSInvocation.

Implementing enum with associated values in Objective-C

Swift has this amazing thing called enum with associated value. Enums are traditionally used to represent a type system with finite set of values. But in real life each of those enum types sometimes have a need to associate some data. And the data of one enum type might be very different than the rest. Imagine writing a parser where each node is of type Node. We usually want to deal with Node as the type for doing things like passing Node around, or generate an array of Node. We might also have methods on Node, such as node.print() but Node can never be instantiated directly as n = Node().

State of Meta Programming in 2020

We all know what meta programming is. We all have tasted that medicine at some point in our lives. The basic idea is that first there is a level of coding we all are familiar with. Usually it is full of fun and excitement. Other times it becomes a bit repetitive, usually when we find ourselves copy pasting some code over and over again and we wish there were an another higher level of code that take over this job.

Objective-C and parsing the unknown

So there’s this pattern I encounter every now and then. There is a service that returns some json data. The json data has some polymorphic behavior built-in. What I mean by that is that there is a root node that describes the behavior of the contained child node, which could be different for every child. To provide an example, lets say we are building a sort of a design system where the components are going to be provided by the server. The client then needs to parse the components and render them on screen.

Implementing callback with NSInvocation

Let’s say we wanted to write a callback with NSInvocation. Why would you need that? Maybe you do not like the block syntax, or maybe you do not like how blocks capturing works, or maybe you’re working with a really old codebase which can not use blocks.