Skip to content

Instantly share code, notes, and snippets.

@fnakstad
Created April 6, 2016 13:18
Show Gist options
  • Save fnakstad/b83cabc704e336835491b86019918321 to your computer and use it in GitHub Desktop.
Save fnakstad/b83cabc704e336835491b86019918321 to your computer and use it in GitHub Desktop.
class FancyString {
let name: String
init () { // Called one time
print("init")
name = "Frederik"
}
deinit { // Called one time
print("deinit")
}
}
class SharedString {
static weak var weakInstance: FancyString?
static var sharedInstance: FancyString? {
get {
if weakInstance == nil {
let newInstance = FancyString()
weakInstance = newInstance
return newInstance
} else {
return weakInstance
}
}
}
}
var str: FancyString? = SharedString.sharedInstance
var str2: FancyString? = SharedString.sharedInstance
var str3: FancyString? = SharedString.sharedInstance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment