Comments (9)
I was also already wondering about thatπ
since we don't have any other
guidance I though best way is to go with gcd ...
On Monday, June 16, 2014, Pietro Rea [email protected] wrote:
What should be the appropriate syntax for declaring singletons in Swift?
Should we still use dispatch_once or does a global constant variable
suffice? Both official books on Swift don't even include the word
"singleton", so we're not getting much guidance from Apple here.β
Reply to this email directly or view it on GitHub
#20.
from swift-style-guide.
I have a feeling it is too early to make a call on this one. Right now I think GCD is the only option.
At the moment Swift doesn't have any indication of atomicity. However, when this arrives i am assuming a singleton can simply be implemented as an atomic static @lazy property.
from swift-style-guide.
Like this:
class MySingleton {
class func sharedInstance() -> MySingleton {
return mySingletonGlobal
}
....
}
let mySingletonGlobal = MySingleton()
The global mySingletonGlobal
is initialized lazily and atomically (using dispatch_once behind the scenes).
Another approach is:
class MySingleton {
class var sharedInstance: MySingleton {
struct Singleton {
static let instance = MySingleton()
}
return Singleton.instance
}
....
}
This does not use a global constant but wraps the initialization into a static member of a nested struct. This also gets initialized lazily and atomically.
There is no need to use GCD directly.
from swift-style-guide.
Ah, I see. There has been some very useful discussion, including those two approaches, on Stackoverflow:
http://stackoverflow.com/questions/24024549/dispatch-once-singleton-model-in-swift
from swift-style-guide.
See also here: https://devforums.apple.com/thread/229436
from swift-style-guide.
I'm still having trouble accepting all these globals. Your first example is nice, but the function is unnecessary because the singleton is already global.
As such, I prefer the second approach, even though it looks uglier/more complex. At least with the second, you need to go through the class to get it, which "feels" more right to me.
That said, I'm fine with whichever approach is more popular.
Sent from my iPhone
On Jun 16, 2014, at 5:51 AM, Matthijs Hollemans [email protected] wrote:
Like this:
class MySingleton {
class func sharedInstance() -> MySingleton {
return mySingletonGlobal
}
....
}let mySingletonGlobal = MySingleton()
The global mySingletonGlobal is initialized lazily and atomically (using dispatch_once behind the scenes).Another approach is:
class MySingleton {
class var sharedInstance: MySingleton {
struct Singleton {
static let instance = MySingleton()
}
return Singleton.instance
}
....
}
This does not use a global constant but wraps the initialization into a static member of a nested struct. This also gets initialized lazily and atomically.There is no need to use GCD directly.
β
Reply to this email directly or view it on GitHub.
from swift-style-guide.
The reason the first example has a sharedInstance()
function is that you want people to access the singleton through that. If they really want to, they can also go through the global constant. You can't really prevent this, but you can discourage it through giving the global an ugly name and by documenting that callers should use sharedInstance()
.
A singleton is an embodiment of global state anyway. If you don't like globals, you shouldn't use singletons. ;-)
I wonder what is nicer, though: a sharedInstance()
function or a sharedInstance
variable:
class MySingleton {
class var sharedInstance: MySingleton {
return mySingletonGlobal
}
// vs
class func sharedInstance() -> MySingleton {
return mySingletonGlobal
}
...
from swift-style-guide.
I realize that. My complaint about the global is the fact that it is a name completely unattached to any type. It's just out there in the app's namespace, rather than contained within the type's namespace. I like at least forcing the caller to go through the type rather than "encouraging" it.
In your two examples there, I would prefer the property. At least you can call it using MySingleton.sharedInstance instead of MySingleton.sharedInstance(), right? No technical reason for that preference, it just feels nicer to me.
BTW, I will prefer this solution over the nested struct thing you showed before once we are allowed private variables, because then you could actually make the singleton variable private rather than this kludgy stuff of having one property return another variable which is you can access freely without that property.
--------- Original Message --------- Subject: Re: [swift-style-guide] Discussion: Singletons? (#20)
From: "Matthijs Hollemans" [email protected]
Date: 6/16/14 10:33 am
To: "raywenderlich/swift-style-guide" [email protected]
Cc: "elephantronic" [email protected]
The reason the first example has a sharedInstance() function is that you want people to access the singleton through that. If they really want to, they can also go through the global constant. You can't really prevent this, but you can discourage it through giving the global an ugly name and by documenting that callers should use sharedInstance().
A singleton is an embodiment of global state anyway. If you don't like globals, you shouldn't use singletons. ;-)
I wonder what is nicer, though: a sharedInstance() function or a sharedInstance variable:
class MySingleton { class var sharedInstance: MySingleton { return mySingletonGlobal } // vs class func sharedInstance() -> MySingleton { return mySingletonGlobal } ... -
Reply to this email directly or view it on GitHub.
from swift-style-guide.
The discussion is a little stale. I'm leaning towards not prescribing how to do singletons in the style guide though, but please file an issue or pull request if you feel strongly otherwise.
from swift-style-guide.
Related Issues (20)
- In Multi-line String Literals, string color is different HOT 2
- Make the linter a dotfile HOT 2
- [email protected] HOT 1
- New line in functions. Not good. HOT 2
- Please add `no_extension_access_modifier` as a default SwiftLint rule HOT 1
- Please Add SwiftUI style guide
- https://ideas.accredible.com/changelog/functionality-for-a-messagenotification-to-be-added-over-an-issued-credential
- edge://credits/ HOT 1
- Clean code
- Function Declarations HOT 1
- 4-space indentation. HOT 2
- False positive from array_constructor custom rule
- how to write switch statement HOT 1
- Preferred type inference of static vars inside extensions HOT 4
- Pls update Readme file to reflect the newest changes in `if let` syntax introduced in Swift 5.7 HOT 1
- unused_import does not seem to be working HOT 1
- Function Calls vs Function Declarations HOT 2
- `unused_import` should be listed in the `analyzer_rules`
- SwiftLint 'attributes' rule flags property wrappers HOT 2
- Function calls format
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swift-style-guide.