I'm developing this app where I want to store user's information that they enter on first screen and be able to use it in multiple application within the app.
In my research I found out that I have to create a singleton. I'm very new to swift so I was hopping someone can guide me.
This is what I have:
class UserInfo {
var userFirstName: String?
var userLastName: String?
}
let sharedInfo = UserInfo()
Am I on right track to create singleton?
I should have written it clearly, but `recently` meant Swift 1.2 (comes with Xcode 6.3) and after.
Till Swift 1.1, I used something like this:
class UserInfo {
class var sharedInfo: UserInfo {
struct My {
static var instance = UserInfo()
}
return My.instance
}
var userFirstName: String?
var userLastName: String?
}
(Sorry, I removed all Xcode 6.1.x installations, and I cannot check if the above code actually works in Xcode 6.1.1 .)
If you don't have some specific reasons, you'd better upgrade your Xcode.