Created
April 6, 2016 13:18
-
-
Save fnakstad/b83cabc704e336835491b86019918321 to your computer and use it in GitHub Desktop.
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
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