Following code works but I was forced by the compiler to use self inside dispatch_sync
, IMO I believe self
isn't created yet and shouldn't be accessible. why self inside a static function?
I was trying to use this way instead
dispatch_sync(queue, {
if (object == nil) {
object = SingleObject()
}
})
return object!
working version
import Foundation
public class SingleObject {
public struct Static {
private static var object: SingleObject?
public static func getObject() -> SingleObject {
let queue = dispatch_queue_create("queue", nil)
dispatch_sync(queue, {
if (self.object == nil) {
self.object = SingleObject()
}
})
return object!
}
}
}
SingleObject.Static.getObject()