A very common pattern across programming languages is the use of callback functions. A callback function is a reference to executable code passed as an argument to other code, allowing it to be executed at a later time.
In swift we encounter this patten often. One particularly common example is…
Recently I have been implementing a lot of analytics events in a client’s app. Tagging this application (which has a great deal of UI) was always going to a laborious task, so I was looking to architect an ergonomic API to interact with.
Recently I discovered that tuples can be used to easily and simply compare and equate multiple variables.
Take for example the implementation of Date and Comparable taken from the Swift documentation:
This compares the year, month and day in that order to determine which is greater (later). A simpler (and perhaps more readable) way of writing this same code is this:
The tuple is compared in order of its variables so we achieve the same result.
As an added bonus, the same is true for Equatable conformance:
This last one is a bit of a contrived example as Equatable conformance would be synthesised for Date anyway, but suppose we wanted to omit the year, to compare birthdays, it would be very easy to simply remove year from the tuples
One of the biggest downsides to UIGestureRecognizer is the inability to attach simple closures in-line. To use them, a target object must be specified alongside a Selector pointing to a function to call.
One major disadvantage to this paradigm is that UIGestureRecognizer only passes the calling object to the called…