Times are changing for iOS application developers. Yes, I’m referring to Apple’s newly announced declarative User Interface (UI) framework, SwiftUI, which will make way for faster, interactive app development. Sure, the iOS platform has come a long way already. If you have worked with the platform close to a decade, like me, you know exactly what I’m talking about. We have progressed from manual memory management (now history, thanks to ARC), single screen sizes, and so on to the modern ARKit in the latest iOS 13. That’s not to say things turned hunky-dory. Consider the pain you go through to get a list of items up and running in Objective C or Swift. With the sheer amount of code that you have to write and the number of methods you have to implement, it’s not surprising if at some point you wondered whether Apple engineers don’t realize how hard it is. If you are an Android developer trying to break into the iOS domain, you might be experiencing some of that frustration too. Building the UI using AppKit and UIKit is a different ball game altogether in the sense that you have a tough time figuring how to piece things together. Well, not anymore.

Declarative Syntax

When Apple announced the programming language Swift five years ago, it was essentially trying to woo developers across platforms with an easier programming style they can relate to. Apple is on the same course with SwiftUI. By using declarative syntax, developers can easily state what the UI should do. This applies not just to a simple list of items but also to complex concepts like animations, thanks to a collection of built-in, ready-to-use effects that developers can use with minimal effort.

Live Previews

Changes to the UI are inevitable during a project’s lifecycle. Whether you are rectifying a typo or aligning the UI components by a pixel or two, you have to recompile the whole project and wait for the simulator to launch to see the changes you made. The wait becomes more excruciating if you are handling a huge project with a bunch of classes and numerous storyboard files. With SwiftUI, the changes you make can be seen almost immediately on the canvas right next to the editor. This is possible since Xcode can now constantly build and run applications by directly switching the edited code in the live app using “dynamic replacement.”

Platform Support

You can use SwiftUI to build UIs for all Apple devices using just one set of tools and API. Be it an iPhone app (including iPhone X, iPhone XS, iPhone XS Max), an iPad app, or Mac apps designed to work with Apple’s latest desktop and notebook computers, SwiftUI can be leveraged by developers to provide solutions across the entire spectrum. Not just this, SwiftUI brings with it a whole new set of features like automatic support for Dynamic Type, Dark Mode (iOS 13), localization (including right-to-left languages) and accessibility.

SwiftUI in Action—Writing Code for a List

I mentioned how complex the code is for a list of items in Objective C (and it’s more or less the same in Swift):
#pragma mark - Table View Data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section {
    return 7;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"CellIdentifier";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                             cellId];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:
                UITableViewCellStyleDefault reuseIdentifier:cellId];
}
       //configure your cell
    cell.textLabel.text = "Some text";
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:
(NSInteger)section{
    NSString *headerTitle;
//Get header title for each section
    return headerTitle;
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:
(NSInteger)section{
    NSString *footerTitle;
//Get footer title for each section
    return footerTitle;
}

#pragma mark - TableView delegate

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath{
   //Do some action
}

@end
This still doesn’t cut it. You still have to configure the table view and cell items if you want to customize the look-and-feel of the list. SwiftUI’s List view, although very similar to UITableView, is wonderfully simple. It has no delegate methods to specify rows, there is no need to dequeue cells and much more. SwiftUI’s lists are composable, which means you can build larger UI elements from smaller ones. In terms of code size, the SwiftUI makes it ridiculously short, not compromising on the look-and-feel, of course. Now see the same or similar listing written with SwiftUI and Lists:
import SwiftUI

struct Item: View {
    
    var content: some View {
        Text("Say Something")
    }
}

struct ContentView: View {
    var body: some View {
        List {
            Section(header: Text("Header")) {
                Item()
            }
        }
    }
}
That’s it! Easy, isn’t it? With minimal code, simpler workflows, interactive design canvas, and one application for all of Apple’s platforms, SwiftUI spells more productivity and better quality applications. But the switch from UIKit and AppKit will be an arduous one, especially for large enterprise applications that are currently in the market. So continued support can be expected until SwiftUI is adopted across the Apple’s developer ecosystem.