Skip to content

Instantly share code, notes, and snippets.

@martin-cotta
Last active October 19, 2015 17:59
Show Gist options
  • Save martin-cotta/9e0bfbdda86f2fca2c3f to your computer and use it in GitHub Desktop.
Save martin-cotta/9e0bfbdda86f2fca2c3f to your computer and use it in GitHub Desktop.
Singleton in Swift
import UIKit
final class Singleton {
static let sharedInstance = Singleton()
private init() { }
var value = ""
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let a = Singleton.sharedInstance
a.value = "a"
let b = Singleton.sharedInstance
b.value = "b"
print(a.value) // b
print(b.value) // b
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment