Contact Us

Use the form on the right to contact us.

You can edit the text in this area, and change where the contact form on the right submits to, by entering edit mode using the modes on the bottom right. 


Oak Ridge, TN, 37830
United States

Swift-Snips

Global Variables - Singletons in Swift

Wade Cantley

The goal is to save a variable to a global parameter, change it, and then call it on another page to show that the change persisted.

But why do it this way?  Well there are a few ways to pass values around but what if I wanted to set the name in one screen and then call it on any view? I could remember to just pass this around between views or maybe stick it into a database or a plist file.  But those are either a pain to deal with or more permanent than I need. 

Where this make sense.

  • You have data that you want to store while moving through the application.
  • You want it to persist for the length of the application and don't care if it gets wiped out when the app is turned off.

 So lets get started.

The project can be downloaded below so I am going to skip through this with the hope that you have some experience in setting up a project.

1) Create two view Controllers called:
Page1ViewController.swift
Page2ViewController.swift

We will get to what we put in these pages in a bit.  

 

2)Create a new project and a storyboard that has two views.  Hook up a couple of buttons that simply take you from one view to the other.  Connect the correct ViewController to the correct page.



 

3) Create a new page called GlobalVariables.swift.

4) Put the following code into GlobalVariables.swift. 


//
//  SingletonManager.swift
//  SingletonExample
//
//  Created by Chris Cantley on 11/25/14.
//  Copyright (c) 2014 Chris Cantley. All rights reserved.
//

class GlobalVariables {
    
    // These are the properties you can store in your singleton
    private var myName: String = "bob"
    
    
    // Here is how you would get to it without there being a global collision of variables.
    // , or in other words, it is a globally accessable parameter that is specific to the 
    // class.
    class var sharedManager: GlobalVariables {
        struct Static {
            static let instance = GlobalVariables()
        }
        return Static.instance
    }
}

What is important to note is that this method should keep the variable parameter safe from any sort of accidental collision.  If I decide to use the parameter name "myName" elsewhere this should not be a problem.

 

5) Put the following code into Page1ViewController.swift. 


//
//  ViewController.swift
//  SingletonExample
//
//  Created by Chris Cantley on 11/25/14.
//  Copyright (c) 2014 Chris Cantley. All rights reserved.
//

import UIKit

class Page1ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // As this page is loaded, just show the value in the variable.
        println("page1 - Current Number = \(GlobalVariables.sharedManager.myName)")
        
        // On returning from page two, you will be able to see that the variable has
        // been changed, showing persistence of that variable across two pages.

        
    }


}


 

5) Put the following code into Page2ViewController.swift. 


//
//  ViewController.swift
//  SingletonExample
//
//  Created by Chris Cantley on 11/25/14.
//  Copyright (c) 2014 Chris Cantley. All rights reserved.
//

import UIKit

class Page2ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Show what it is now
        println("page2 - Current Number = \(GlobalVariables.sharedManager.myName)")

        // Change it
        GlobalVariables.sharedManager.myName = "Chris"
        
        // Show what it is after the change.
        println("page2 - Changed Number = \(GlobalVariables.sharedManager.myName)")
        
    }
    
    
}


So lets try this out.

We run the app and by default "bob" is set up and displayed on page 1.

Then we click on the "Next Page" and see that the name has been changed from "bob" to "Chris"

And lastly, we click on "Back Page" to find that the change has persisted.

That's it.  
Here is a link to the repository.