Skip to content

Instantly share code, notes, and snippets.

@topherPedersen
Last active August 7, 2018 07:16
Show Gist options
  • Select an option

  • Save topherPedersen/959c335fa6a1c2129c6100e7f2c822f1 to your computer and use it in GitHub Desktop.

Select an option

Save topherPedersen/959c335fa6a1c2129c6100e7f2c822f1 to your computer and use it in GitHub Desktop.
Implementing an MVC Architecture in iOS using a Singleton Pattern (written in Swift)
// ------------------------------------------------------------------------------------->
// This example is comprised of three swift files demonstrating how to implement an MVC
// architecture using the singleton pattern in iOS. By using a singleton pattern one can
// create a simple class (or classes) to serve as the model for an application
// and ensure that only one instance of that class is created and that it is accessible
// globally. We do this by creating a 'shared' property to make access to the single
// instance global, and initialize the class privately to ensure that only one instance
// is ever created. While creating this simple app/gist I used the following blog post
// from Bart Jacobs at cocoacasts.com for guidance (Thanks Bart!):
//
// "What is a Singleton How to Create One in Swift"
// https://cocoacasts.com/what-is-a-singleton-and-how-to-create-one-in-swift
//
// ------------------------------------------------------------------------------------->
//
// SingletonModel.swift
// SingletonApp
//
// Created by Christopher Pedersen on 8/6/18.
// Copyright © 2018 Christopher Pedersen. All rights reserved.
//
import Foundation
class SingletonModel {
// Declare a 'shared' property which will allow a single
// instance of SingletonModel to be used globally anywhere
// within our application.
static let shared = SingletonModel()
// Create a property to store application state in our model
var myProperty: String
// Privately initialize SingletonModel so that multiple
// instances cannot be created. Using this singleton
// design allows us to create a single global which will
// serve as the model for our application.
private init() {
self.myProperty = "FOO"
}
}
//
// ViewController.swift
// SingletonApp
//
// Created by Christopher Pedersen on 8/6/18.
// Copyright © 2018 Christopher Pedersen. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
mainLabel.text = SingletonModel.shared.myProperty
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBOutlet weak var mainLabel: UILabel!
}
//
// SecondaryViewController.swift
// SingletonApp
//
// Created by Christopher Pedersen on 8/6/18.
// Copyright © 2018 Christopher Pedersen. All rights reserved.
//
import Foundation
import UIKit
class SecondaryViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// mainLabel.text = SingletonModel.shared.myProperty
SingletonModel.shared.myProperty = "BAR"
secondaryLabel.text = SingletonModel.shared.myProperty
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBOutlet weak var secondaryLabel: UILabel!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment