Using a dispatch_once singleton model in Swift -


i'm trying work out appropriate singleton model usage in swift. far, i've been able non-thread safe model working as:

class var sharedinstance:tpscopemanager {     {         struct static {             static var instance : tpscopemanager? = nil         }          if !static.instance {             static.instance = tpscopemanager()         }          return static.instance!     } } 

wrapping singleton instance in static struct should allow single instance doesn't collide singleton instances without complex naming schemings, , should make things private. though, model isn't thread safe, tried add dispatch_once whole thing:

class var sharedinstance:tpscopemanager {     {         struct static {             static var instance : tpscopemanager? = nil             static var token : dispatch_once_t = 0         }          dispatch_once(static.token) { static.instance = tpscopemanager() }          return static.instance!     } } 

but compiler error on dispatch_once line:

cannot convert expression's type 'void' type '()'

i've tried several different variants of syntax, seem have same results:

dispatch_once(static.token, { static.instance = tpscopemanager() }) 

what proper usage of dispatch_once using swift? thought problem block due () in error message, more @ it, more think may matter of getting dispatch_once_t correctly defined.

tl;dr: use class constant approach if using swift 1.2 or above , nested struct approach if need support earlier versions.

from experience swift there 3 approaches implement singleton pattern support lazy initialization , thread safety.

class constant

class singleton  {    static let sharedinstance = singleton() } 

this approach supports lazy initialization because swift lazily initializes class constants (and variables), , thread safe definition of let. officially recommended way instantiate singleton.

class constants introduced in swift 1.2. if need support earlier version of swift, use nested struct approach below or global constant.

nested struct

class singleton {     class var sharedinstance: singleton {         struct static {             static let instance: singleton = singleton()         }         return static.instance     } } 

here using static constant of nested struct class constant. workaround lack of static class constants in swift 1.1 , earlier, , still works workaround lack of static constants , variables in functions.

dispatch_once

the traditional objective-c approach ported swift. i'm there's no advantage on nested struct approach i'm putting here anyway find differences in syntax interesting.

class singleton {     class var sharedinstance: singleton {         struct static {             static var oncetoken: dispatch_once_t = 0             static var instance: singleton? = nil         }         dispatch_once(&static.oncetoken) {             static.instance = singleton()         }         return static.instance!     } } 

see github project unit tests.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -