I learned Swift from the ground up so I could program the app of my dreams. Let’s talk about global variables.

I had several view controllers in my app, and I was searching for a way to use common variables among them.  These are informally called global variables.  Swift makes it surprisingly simple to incorporate global variables into your program.  In the example below, simply define the variable OUTSIDE of the view controller classes.


Lines 10 & 11 define the global variables which we can use in the two view controllers.

It is the same principle behind the “import UIKit” command on line 8. UIKit is essentially a set of classes, variables and functions exclusively geared towards iOS development. When you import this Framework your app will have access to functions controlling things like the accelerometer, screen swipes, device rotation, camera, and other aspects relating to portable Apple devices.

The view controllers can use any of the UIKit commands, just like they can use our new global variables.

To be honest, this approach to global variables is generally looked upon as bad form.  It is okay if you just have a couple of variables, but what happens when there are two dozen, scattered about through several files?!  Who can find them or know what they are?  You would be surprised when you have been away from your own code for a couple weeks how you have to retrace your steps to figure out what is what. Keeping your global variables all together makes this much easier to do.

Therefor, encapsulate your global variables into their own class.  This keeps everything all in one place, and gives you the added benefit of XCodes’ handy autocomplete.  Let’s turn the example above into one with better form and see what happens:


On line 10 we have a class called “Global” which actually can contain all the variables you want!  Then on line 15 we make the variable “global,” an instance of the Global class, which we can use throughout the rest of the program.  Best of all, when you type in “global.” Xcode autocomplete will give you a list of all the sub variables in the Global class due to dot syntax formatting.

It is important to note that defining a class, as in line 10, is only like writing the blueprints to a house.  You don’t actually have a house to work with until you initialize it, as in line 15. A class is simply the set of instructions which say what a thing will do, or what parts it has. The instance is the object itself, the one you work with in the program. It is good form to name classes with a capital letter, and to name an instance of a class with a lower case letter. Instances don’t have to be named the same thing as a class at all, but often are simply because it makes it easier to remember.

An interesting thing has happened though, what we actually have is the beginning of an app database!

The Apple documentation recommends an app structure called MVC:

The Model-View-Controller (MVC) design pattern assigns objects in an application one of three roles: model, view, or controller. The pattern defines not only the roles objects play in the application, it defines the way objects communicate with each other.


The Model is the data the app uses. In Facebook the Model would include your specific posts, information, and pictures. The Model are the things which make your FB page different from everyone elses. In our example the Model is our global instance of the Global class.

The View is the user interface, the parts people actually see. For our FB example the view is the arrangement of posts, where buttons are, profile picture, and uniform features such as Like and Comment for every post. It is the user interface that you look at when you use the FB app. The data in the View will be different depending on which friend is up, but the layout for all friends and how you interact with it is the same. In Xcode we define the View using the Storyboard. But for what we have done here of course we have not gotten into the Storyboard.  We’ll get into that in a later post.

The Controller is the behind the scenes actions which are defined in the program. This is all the programming which determines what happens when you press a button, enter a URL or swipe through the app. It also integrates the data into the view. So for FB the Controller is all the programming which puts the posts in order, updates the database when a picture gets a new Like, and switches to the photo screen when you press the Photos button.  In Xcode, our two view controller classes define all the behind the scenes action of the associated view controllers in the Storyboard.  Just like global is an instance of the Global class, The drag and drop view controllers in the Storyboard will be the instances of MyFirstViewController and MySecondViewController. (Even though we don’t actually have anything in the storyboard yet…)

Let’s just change the name of our global class and variable and you can see what happens:


Swift is supposed to make programming smoother and more intuitive. Get out there and use those global variables, or even better, a central class containing all your database information.

Thanks for tuning in. Make me proud.