Appendix B. Some Useful Utility Functions

As you work with iOS and Swift, you’ll develop a personal library of frequently used convenience functions. Here are some of mine. Each of them has come in handy in my own life; I keep them available as snippets in Xcode’s Library, so that I can use them in any project. Many of them have been mentioned earlier in this book.

Launch Without Main Storyboard

If an app lacks a main storyboard, or if you want to ignore the main storyboard and generate the app’s initial interface yourself, it’s up to you to configure the window and supply the root view controller (“How an App Launches”). A minimal app delegate class would look something like this:

@UIApplicationMain
class AppDelegate : UIResponder, UIApplicationDelegate {
    var window : UIWindow?
    func application(_ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions:
        [UIApplication.LaunchOptionsKey : Any]?) -> Bool {
            self.window = self.window ?? UIWindow()
            self.window!.backgroundColor = .white
            self.window!.rootViewController = ViewController()
            self.window!.makeKeyAndVisible()
            return true
    }
}

Core Graphics Initializers

The Core Graphics CGRectMake function needs no argument labels when you call it. But Swift cuts off access to this function, leaving only the various CGRect initializers, all of which do need argument labels. However, we know what each argument signifies, so why clutter the call with labels? The solution is a CGRect initializer without labels (Chapter 1):

Get Programming iOS 12 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.