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. Each 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
The Core Graphics CGRectMake
function needs no argument labels when you call it. Swift cuts off access to this function, leaving only the various CGRect initializers, all of which do need argument labels. That’s infuriating. We know what each argument signifies, so why clutter the call with labels? The solution is another CGRect initializer, without labels:
extension CGRect { init(_ x:CGFloat, _ y:CGFloat, ...
Get Programming iOS 11 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.