Last active
October 19, 2015 17:59
-
-
Save martin-cotta/9e0bfbdda86f2fca2c3f to your computer and use it in GitHub Desktop.
Singleton in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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