www.robkerr.com
www.robkerr.com

mobile development, cloud computing and building great software

Rob Kerr
Author

Addicted to coding since writing my first programs for the Commodore computers in the 1980s. Currently working as an independent contractor focused on native iOS development.

Share


Tags


Twitter


Accessing AppDelegate in swift 1...3

Rob KerrRob Kerr

We all learned in uni that global variables are undesirable, but most iOS applications need to access some global members of the shared UIApplication object.

I appreciate that the syntax is getting better and better, but it's still a moving target and challenges my noodle to keep up.

Here's a quick cheat sheet on accessing AppDelegate through the versions. In each example, we're getting our shared appDelegate to check a variable called isProduction

Swift 3

let appDelegate = UIApplication.shared.delegate as! AppDelegate
let isProd = appDelegate.isProduction

Swift 1.2..<3

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let isProd = appDelegate.isProduction

Swift 0..<1.2

let appDelegate = UIApplication.sharedApplication as AppDelegate
let isProd = appDelegate.isProduction
Rob Kerr
Author

Rob Kerr

Addicted to coding since writing my first programs for the Commodore computers in the 1980s. Currently working as an independent contractor focused on native iOS development.

Comments