Appendix B. Some Useful Utility Functions

As you work with iOS and Swift, you’ll doubtless develop a personal library of frequently used convenience functions. Here are some of mine. Every one of them has come in handy in my own life; I keep them available as user snippets in Xcode so that I can paste them into any project.

Launch Without Main Storyboard

As I explained in Chapter 1, if an app lacks a main storyboard, or if you want to ignore the main storyboard and generate the app’s initial interface yourself, configuring the window and supplying the root view controller is up to you. A minimal app delegate class would look something like this:

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

Core Graphics Initializers

C and Objective-C have the Core Graphics CGRectMake function, which needs no argument labels when you call it. Infuriatingly, Swift 3 cuts off access to this function, leaving only the various CGRect initializers, all of which do need argument labels. I find those labels otiose, clumsy, and verbose; we know what each argument signifies, so why clutter the call with labels? The solution is another ...

Get Programming iOS 10 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.