Articles, podcasts and news about Swift development, by John Sundell.

Introducing Objective-C UI

Published on 01 Apr 2020

⚠️ In case you’re finding this after April 1st: This whole article is an April Fools’ joke and is not meant to be taken seriously in any way. You can find hundreds of serious articles here though.

SwiftUI is truly a game-changer when it comes to building apps for Apple’s platforms. With its declarative DSL, powerful data binding capabilities, and composable design — it’s fair to say that SwiftUI marks the beginning of a new era in the Apple developer community.

However, as I’ve been using SwiftUI since it was first released during WWDC 2019, I had to ask myself the question — what would it be like if we had SwiftUI, without the baggage of Swift? Well, I did more than think about it. I have a new framework to announce…

Introducing Objective-C UI.

Rewritten from the ground up

Objective-C UI combines the power of SwiftUI with the beautiful syntax of the Objective-C programming language. Gone are the strange function builders, the lack of return keywords and the confusion of type inference. Instead, Objective-C UI doubles down on the perhaps best feature of SwiftUI — all the @ signs!

Let’s jump right into an example. Here’s how we can easily declare a ”Hello, world” ContentView using Objective-C UI:

@interface ContentView: OCUIView

@end

@implementation ContentView

- (OCUIView *)body {
    return [OCUILabel labelWithText:@"Hello, world!"];
}

@end

As you can see above, Objective-C UI doesn’t simply copy SwiftUI’s API directly — instead it has been completely rewritten to feel right at home within Objective-C.

For example, in Swift, the above label would be declared as Text("Hello, world!") — which is really hard to understand. What is this Text type and where does it come from? What does the string passed into it really do?

The Objective-C version, on the other hand, is crystal clear. The OCUI prefix instantly tells us that this type comes from Objective-C UI, the labelWithText method reminds us yet again (in case we forgot) that we’re creating a label, and the @ sign right before the string beautifully separates the string from our method call.

But that’s just the beginning.

Reimagining the essence of SwiftUI

Objective-C UI also improves SwiftUI’s stacks and spacers with a beautiful new API, completely reimagined for Objective-C. In fact, it uses the exact same layout system as SwiftUI itself, but turns the verbosity up to 11. VStack becomes OCUIVerticalStack, which we can use to push the label we created earlier to the top of the screen — like this:

@implementation ContentView

- (OCUIView *)body {
    return [OCUIVerticalStack stackWithViewsCreatedByBlock:^NSArray<OCUIView *> *{
        return @[
            [OCUILabel labelWithText:@"Hello, world!"],
            [OCUISpacer spacer]
        ];
    }];
}

@end

Next, let’s take a look at modifiers, which get the same treatment as the rest of the SwiftUI API — with lots of delicious verbosity, and (most importantly) lots of wonderful square brackets:

@implementation ContentView

- (OCUIView *)body {
    return [OCUIVerticalStack stackWithViewsCreatedByBlock:^NSArray<OCUIView *> *{
        return @[
            [[[OCUILabel labelWithText:@"Hello, world!"]
                labelByChangingFontTo:[OCUIFont fontForTitles]]
                labelByChangingTextColorTo:[OCUIColor
                    colorWithRed:0.2
                    andGreen:0.5
                    andADashOfBlue:0.1
                ]
            ],
            [OCUISpacer spacer]
        ];
    }];
}

@end

Finally, Objective-C UI also ships with full support for all of SwiftUI’s data binding capabilities — including property wrappers like @State. To make this happen, Objective-C UI uses the ultimate property wrapper — Objective-C’s @property keyword — along with an innovative block-based API that takes UI data binding to a whole new level.

Here’s how we could use those capabilities to bind a text field to a title property, which we then render using a label:

@interface ContentView: OCUIView

@property (nonatomic, strong) OCUIState<NSString *> *title;

@end

@implementation ContentView

- (instancetype)initWithTitle:(NSString *)title {
    self = [super init];

    if (self) {
        _title = [OCUIState stateWithDefaultValue:title];
    }

    return self;
}

- (OCUIView *)body {
    __weak typeof(self) weakSelf = self;

    return [OCUIVerticalStack stackWithViewsCreatedByBlock:^NSArray<OCUIView *> *{
        typeof(self) strongSelf = weakSelf;

        return @[
            [OCUITextField textFieldBoundToState:strongSelf.title],
            [strongSelf.title viewUsingBindingBlock:^OCUIView *(NSString *title) {
                return [OCUILabel labelWithText:title];
            }]
        ];
    }];
}

@end

Isn’t that just wonderful?

So, should I adopt Objective-C UI?

Now, when reading this article, you must have been thinking “I can’t wait to adopt this!”, and that’s cool. However, Objective-C UI is not for everyone, so to help you decide whether or not to adopt it, here’s who I think Objective-C UI is best suited for:

Conclusion

Objective-C UI takes the best parts of SwiftUI and combines them with the power and verbosity of Objective-C, to bring UI development for Apple’s platforms to a whole new level. I can’t wait to see what you’ll do with it.

Thanks for reading! 🚀

This whole article is an April Fools' joke. During these difficult times, I thought we could all use a little bit of a distraction and a laugh, and I hope you enjoyed this article. I’m personally still a fan of Objective-C (although I don’t think it’s a very good fit for SwiftUI’s DSL 😅), and I have a ton of real SwiftUI content available right here.