Vatsal Manot

Read this first

Data Flow Through SwiftUI

This article may be considered a condensed version of the (highly-recommended) WWDC19 talk, “Data Flow Through SwiftUI”, along with a few thoughts and insights of my own. It represents my own, current understanding of data flow in SwiftUI, derived from experimentation over the past two months.

Let’s start with an important quote from the aforementioned talk:

Data is a first class citizen in SwiftUI.

This is the crux of what makes SwiftUI not only so beautifully elegant, but also extremely ergonomic.

The Two Key Principles

SwiftUI is designed to be declarative and functional. As such, data flow in SwiftUI revolves around two key principles:

  • Reading data creates dependencies.
  • Every piece of data has a source of truth.

This all seems quite abstract, and so we shall revisit a popular definition for declarative programming:

“You know, imperative programming is like* how you do...

Continue reading →


For Antara

The mellow embrace of blue,
With a silent whisper of white,
Dipped gently in lavender,
Flowing fill into sight.

The quiet stretch of infinity,
Peering deep into the soul,
An ocean of serenity,
Washing away the toll.

An impossible canvas beheld,
Each stroke brushed past,
Dark painted darker,
A memory etched last.

Persuasive, pristine, perfect,
A shapeless void in flight;
That, my dear,
Is the beauty of night.

View →


Reimplementing SwiftUI’s deprecated relative view sizing functions.

With Xcode 11 beta 4, Apple deprecated SwiftUI’s View.relativeSize function (as well as View.relativeWidth and View.relativeHeight).

The relativeWidth(_:), relativeHeight(_:), and relativeSize(width:height:) modifiers are deprecated. Use other modifiers like frame(minWidth:idealWidth:maxWidth:minHeight:idealHeight:maxHeight:alignment:) instead. (51494692)

For those dismayed by these changes, there’s an easy reimplementation available using GeometryReader:

extension View {
    public func relativeHeight(
        _ ratio: CGFloat,
        alignment: Alignment = .center
    ) -> some View {
        GeometryReader { geometry in
            self.frame(
                height: geometry.size.height * ratio,
                alignment: alignment
            )
        }
    }

    public func relativeWidth(
        _ ratio: CGFloat,
        alignment: Alignment = .center
    ) -> some View {
...

Continue reading →


PresentationLink broken in Xcode 11 beta 3

PresentationLink (in SwiftUI) seems to be broken in Xcode 11 beta 3.

It succeeds in presenting a modal view controller the first time, but stops working thereafter.

As noted in this StackOverflow post, this seems to occur when embedding a PresentationLink within a NavigationView, with the following warning printed to console:

[WindowServer] display_timer_callback: unexpected state (now:1abc3d3ccc7 < expected:1abc3d91a0f)

This bug has existed since Xcode 11 beta 1 (back when PresentationLink was called PresentationButton), which is quite alarming given the fact that we’re on beta 3 now.

I’ve written a quick and dirty workaround for this:

private enum SetPresentedViewKey: EnvironmentKey {
    static var defaultValue: (AnyView?) -> () {
        fatalError()
    }
}

private extension EnvironmentValues {
    var setPresentedView: (AnyView?) -> () {
        get {
...

Continue reading →


Some Plane Poetry

I’m stuck in a plane,
Soaring dreamless through the night.

I’m stuck in a plane,
Flying aimless out of sight.

I’m stuck in a plane,
My thoughts are stuck too.

I’m stuck in a plane,
This poem sucks,
Boo.

View →


3D Touch

3D Touch is a woefully underrated feature in today’s modern iPhones. There are now (what I consider to be) credible reports suggesting that Apple intends to drop this feature in their 2019 iPhone models.

This is highly unfortunate if true.

What is 3D Touch?

Ultimately, this is our focus. 3D Touch is something we’ve worked on for a long time—multi, multi, multi years

3D Touch was introduced circa 2015 with the iPhone 6S. It’s the extra informational input of pressure with every touch on the screen. Apparently it was something they’d been working on for a while.

Why is going away?

Simply put - it failed to become an idiomatic element of the iPhone’s UX language.

Many have speculated on why this is so. Here are the top 5 reasons that I have surmised:

  1. A lack of marketing.
  2. Inconsistent availability across iOS.
  3. Lack of any visual cues to enable discovery.
  4. Lack of developer...

Continue reading →


Songs of the Week #3

I’ve been putting off writing in favor of professional crastination, but no more! This is technically week 5 or 6, but “mah blog mah rulez” - I’m going to call it week 3.

Boasty (feat. Idris Elba) by Wiley, Stefflon Don & Sean Paul

The bounce on this song is absolutely fantastic. This is now my new favorite Jamaican hip-hop/rap beat, and Idris Elba is fantastic in whatever he does.

Dance Off by Macklemore & Ryan Lewis from “This Unruly Mess I’ve Made”

I came across this while searching for more Idris Elba (after Boasty), and it’s quite a charged song. Love the lyrics, love the beat.

I Need Dubs by Lil’ Romeo

A smooth song. I’m adding this to my “Pimping” playlist.

You’ll Never Hear From Me Again by David Schwartz from Arrested Development

Arrested Development has been a long time favorite of mine in TV sitcoms, and the rest of the soundtrack is great as well. While I’m sure...

Continue reading →


Songs of the Week #2

Love Is a Beautiful Thing by Theo Katzman

There are actually three versions of this song that I wholeheartedly recommend:

  • Version 1 - the original
  • Version 2 - a capella
  • Version 3 - with Vulpeck and Monica Martin - my favorite by far

If by Kat Edmonson from “Old Fashioned Gal”

This is is a beautiful track that should’ve made it in last last week’s list. I discovered it while watching Russian Doll, and what struck me right off the bat is The Ink Spots’s leitmotif. I’ve been a huge Ink Spots fan ever since my introduction to Address Unknown through Better Call Saul, and hearing that opening gave me goosebumps.

Kat Edmonson has her patented style of music that she calls “vintage pop”, and it’s esentially exactly what it sounds like. What’s amusing is that the first time I heard this song (and when Shazam brought up the covert art), I actually googled to see whether The Ink...

Continue reading →


Songs of the Week #1

Finally starting this, it’s been long overdue.

  • All Your Love - Single by Jakob Ogawa
  • City of Stars / May Finally Come True (feat. Ryan Gosling & Emma Stone) by Justin Hurwitz
  • Feels by Snoh Aalegra
  • Vienna by Billy Joel

“Vienna”, in particular, struck a chord (pun intended). It’s going down under the list of songs that ‘capture me’.

View →


Context capturing C function pointers in Swift.

Take the following code:

func foo() {
    let bar = NSObject()

    let f: (@convention(c) () -> ()) = {
        print(bar)
    }
}

This will not compile. You will instead be presented with the following error:

error: a C function pointer cannot be formed from a closure that captures context

While you are unlikely to ever encounter this error in typical iOS development, it may arise as the result of, say, an attempt to interface with a low-level C library/framework. In my case, I was trying to construct a parameter for a low-level POSIX function (pthread_create), which only accepted a C function pointer.

There is a (dirty) workaround:

import ObjectiveC
import Swift

func cFunction(_ block: (@escaping @convention(block) () -> ()))
    -> (@convention(c) () -> ()) {
    return unsafeBitCast(
        imp_implementationWithBlock(block),
        to: (@convention(c) () -> ()).self
...

Continue reading →