O'Reilly logo

Programming iOS 10 by Matt Neuburg

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required