Singleton with properties in Swift 3 - singleton

In Apple's Using Swift with Cocoa and Objective-C document (updated for Swift 3) they give the following example of the Singleton pattern:
class Singleton {
static let sharedInstance: Singleton = {
let instance = Singleton()
// setup code
return instance
}()
}
Let's imagine that this singleton needs to manage a variable array of Strings. How/where would I declare that property and ensure it gets initialized properly to an empty [String] array?

For me this is the best way, make init private. Swift 3 \ 4 \ 5 syntax
// MARK: - Singleton
final class Singleton {
// Can't init is singleton
private init() { }
// MARK: Shared Instance
static let shared = Singleton()
// MARK: Local Variable
var emptyStringArray = [String]()
}

You can initialize an empty array like this.
class Singleton {
//MARK: Shared Instance
static let sharedInstance : Singleton = {
let instance = Singleton(array: [])
return instance
}()
//MARK: Local Variable
var emptyStringArray : [String]
//MARK: Init
init( array : [String]) {
emptyStringArray = array
}
}
Or if you prefer a different approach, this one will do fine.
class Singleton {
//MARK: Shared Instance
static let sharedInstance : Singleton = {
let instance = Singleton()
return instance
}()
//MARK: Local Variable
var emptyStringArray : [String]? = nil
//MARK: Init
convenience init() {
self.init(array : [])
}
//MARK: Init Array
init( array : [String]) {
emptyStringArray = array
}
}

As per the apple's documentation: In Swift, you can simply use a static type property, which is guaranteed to be lazily initialized only once, even when accessed across multiple threads simultaneously.
class Singleton {
// MARK: - Shared
static let shared = Singleton()
}
With initialization method:
class Singleton {
// MARK: - Shared
static let shared = Singleton()
// MARK: - Initializer
private init() {
}
}

Any initialisation would be done in an init method. No difference here between a singleton and a non-singleton.

Related

Object constructor and Delegates.notNull

object WalletConfig {
init {
fill(HashMap<String, String>())
}
var clientUrl: String by Delegates.notNull()
private set
fun fill(settingMap: Map<String, String>) {
try {
clientUrl = settingMap["URL_CLIENT"] ?: ""
} catch(ex:Exception) { }
}
}
throw ex: fill: Load 'wallet.config' config file: java.lang.NullPointerException at ru.WalletConfig.setClientUrl(WalletConfig.kt)
The problem is caused by the miss ordered init block and property initializer. As stated in the answer about class initialization semantics. The init block is not the constructor - its code is included in the primary constructor.
The following example:
class OrderOfInits {
constructor() {
println("constructor")
}
init {
println("A")
}
init {
println("B")
}
init {
println("C")
}
}
val a = OrderOfInits()
Would print:
A
B
C
constructor
Property delegates initialization is also part of primary constructor. Their initialization order in constructor reflects the order they were declared in. In other words the init { fill(...) } initializer block invokes clientUrl setter which in turn tries to call Delegates.notNull() instance method. However the instance field holding the Delegates.notNull() was not yet set hence NPE.
To fix the problem you just need to reorder them like so:
object WalletConfig {
var clientUrl: String by Delegates.notNull()
private set
init {
fill(HashMap<String, String>())
}
...
}

Swift 3 - Singleton

class MyManager {
private static var __once: () = {
Static.instance = MyManager()
}()
class var sharedInstance: MyManager {
struct Static {
static var onceToken: Int = 0
static var instance: MyManager? = nil
}
_ = MyManager.__once
return Static.instance!
}
fileprivate init() {
print("MyManager init");
}
....... etc
calling it
aManager = MyManager.sharedInstance
results in
MyManager init fatal error: unexpectedly found nil while unwrapping an Optional value
_ = MyManager.__once isn't calling your __once function, it's assigning it to nothing. You forgot the ():
MyManager.__once()
That's the whole purpose of requiring _ =, to make you realize you're dealing with the function itself, not a function call.
Regardless, this is an unnecessarily convoluted and messy implmentation of a singleton. All you need is:
class MyManager {
static let instance = MyManager()
}
It's lazy, thread-safe, and sane.
For me this is the best way, make init private.
// MARK: - Singleton
final class Singleton {
// Can't init is singleton
private init() { }
// MARK: Shared Instance
static let shared = Singleton()
// MARK: Local Variable
var emptyStringArray : [String] = []
}

Having a singleton use the value from initWithCoder instead of creating a new instance in Swift

I have a simple boolean variable in a function on which a set an observer to check when it changes:
var userValid: Bool{
didSet{
let valid=userValid ? "valid" : "not valid"
print("uservalid changed to \(valid)")
}
}
I set it to false in the init and then to true in the program: the initialiser is called twice once for the class and once for the subclass but I am alway checking the value in the class. So I am seeing the variable is set to true in the observer, but then when I check it in another function I find it at false without the observer to be ever called again.
As it comes out the problem seems to stem from the fact I initialise two times the class: once as a singleton and the other one with:
init?(coder aDecoder: NSCoder)
Of course I initialise the singleton with:
public class func sharedMapDelegate() -> MapDelegate {
struct Static {
static let instance : MapDelegate = MapDelegate()
}
return Static.instance
}
How may I return the value generated from the initWithCoder instead of creating a new one?
I think I have fixed it by changing the singleton to:
public class func sharedMapDelegate() -> MapDelegate {
struct Static {
static var instance : MapDelegate {
if let me = Factory.sharedFactory().mapHandler {
return me
} else {
Factory.sharedFactory().mapHandler=MapDelegate()
return Factory.sharedFactory().mapHandler
}
}
}
return Static.instance
}

ReactiveCocoa: creating a signal that applies a map over an observer

From what I understand, the RACSubject equivalent of ReactiveCocoa 4 is the Observer class.
I want to have a signal and an observer linked together so that the signal sends events applying a map operation to the events sent to the observer.
In Obj-C it looks like this:
// ViewModel.h
#interface ViewModel
#property (readonly) RACSubject *someAction; //expects e.g. int values
#property (readonly) RACSignal *someSignal; //sends e.g. string values
#end
// ViewModel.m
//redeclaring the signal and action as readwrite
#implementation
- (id)init {
_someAction = [RACSubject subject];
_someSignal = [_someAction map:^id(NSNumber *index) {
return "Some string based on index passed in";
}];
}
#end
Now when someone pushes a value onto someAction, the someSignal will fire an event containing a derived value.
How do I achieve the same effect in Swift?
What I've been able to do so far is something like this:
public class ViewModel: NSObject {
public let (internalSignal, someAction) = Signal<Int, NoError>.pipe()
public var someSignal: Signal<String, NoError> {
get {
return self.internalSignal.map({ [unowned self](index: Int) -> String in
return "Some string value based on \(self.someArray[index])"
})
}
}
public let someArray = [1, 2, 3, 4, 5]
}
Which looks like a bad solution because
internalSignal should be private but needs to be declared public in order to match it to Signal's pipe
someSignal is computed every time it's needed therefore, even though the same signal could be reused over and over. Also can't be declared as a let constant.
You could initialize the members in init just like ObjC...
public class ViewModel: NSObject {
private let internalSignal: Signal<Int, NoError>
public let someAction: Observer<Int, NoError>
public let someSignal: Signal<String, NoError>
override init() {
(internalSignal, someAction) = Signal<Int, NoError>.pipe()
someSignal = internalSignal.map { index in
"Some string value based on \(index)"
}
super.init()
}
}
For someSignal you could also use lazy initialization, which allows the member to refer to self:
public class ViewModel: NSObject {
private let internalSignal: Signal<Int, NoError>
public let someAction: Observer<Int, NoError>
public private(set) lazy var someSignal: Signal<String, NoError> =
self.internalSignal.map { [unowned self] index in
"Some string value based on \(self.someArray[index])"
}
override init() {
(internalSignal, someAction) = Signal<Int, NoError>.pipe()
super.init()
}
}
Unlike the first piece of code, the lazy-var is initialized only before someSignal is used, not at the ViewModel's initialization.
Also, since it is a var, Swift allows you use mutate its value (there is no such thing as lazy let). We can restrict the permission using private(set), but this won't prevent you accidentally write self.someSignal = ... somewhere.
Alternatively, you could make someSignal an implicitly unwrapped optional and initialize manually:
public class ViewModel: NSObject {
private let internalSignal: Signal<Int, NoError>
public let someAction: Observer<Int, NoError>
public private(set) var someSignal: Signal<String, NoError>!
override init() {
(internalSignal, someAction) = Signal<Int, NoError>.pipe()
super.init()
someSignal = internalSignal.map { [unowned self] index in
"Some string value based on \(self.someArray[index])"
}
}
}

Alternative to load method in Swift

I am working on developing an application in Swift. I wanted to design a system for the application that allowed for loose coupling between objects, and one strategy (which I have used successfully in other languages) was to create something I call an instance factory. It is pretty simple and here is the basic implementation I came up with in Swift:
import Foundation
private var typeGenerators = Dictionary<String, InstanceFactory.GeneratorCallback>()
public class InstanceFactory: NSObject {
public typealias GeneratorCallback = () -> AnyObject!
public class func registerGeneratorFor(typeName: String, callback: GeneratorCallback) {
typeGenerators[typeName] = callback
}
public class func instanceOf(typeName: String) -> AnyObject! {
return typeGenerators[typeName]?()
}
}
The idea is that when an object instance needs access to another object instance, rather than creating that instance outright which would more tightly couple the two objects, the first object would defer to the factory to provide the needed instance by calling the instanceOf method. The factory would know how to provide various instance types because those types would register with the factory and provide a closure that could generate the instance.
The trick is how to get the classes to register with the factory. I had previously made a similar factory in Objective-C and the way I got registration to work was to override the +load method for each class that needed to register with the factory. This worked great for Objective-C, and I figured it could work for Swift as well since I would be restricting the factory to only provide objects that are derived from NSObject. It appeared I got this to work and I spent a significant about of effort designing classes to make use of the factory.
However, after upgrading to Xcode 6.3, I discovered Apple has disallowed the usage of the load class method in Swift. Without this, I am unaware of a mechanism to allow classes to automatically register themselves with the factory.
I am wondering if there some other way to get the registration to work.
What alternatives are available that could allow classes to register with the factory, or what other techniques could be use to accomplish the same kind of loose coupling the factory provides?
I've found a possible solution to your problem after I wanted to register all ViewControllers that would be implementing a certain Protocol in my application and I ran into both this question and a possible answer.
The original was posted here: How to list all classes conforming to protocol in Swift?
I adapted it to Swift 3 and made it a bit more Swift-y and generic:
import UIKit
class ContextRoute: NSObject {
}
#objc protocol ContextRoutable {
static var route: ContextRoute { get }
}
class ContextRouter: NSObject {
private static var storedRoutes: [ContextRoute]?
static var routes: [ContextRoute] {
get {
if let storedRoutes = storedRoutes {
return storedRoutes
}
let routables: [ContextRoutable.Type] = classes(implementing: ContextRoutable.self)
let newRoutes = routables.map { routable in routable.route }
storedRoutes = newRoutes
return newRoutes
}
}
private class func classes<T>(implementing objcProtocol: Protocol) -> [T] {
let classes = classList().flatMap { objcClass in objcClass as? T }
return classes
}
private class func classList() -> [AnyObject] {
let expectedClassCount = objc_getClassList(nil, 0)
let allClasses = UnsafeMutablePointer<AnyClass?>.allocate(capacity: Int(expectedClassCount))
let autoreleasingAllClasses = AutoreleasingUnsafeMutablePointer<AnyClass?>(allClasses)
let actualClassCount:Int32 = objc_getClassList(autoreleasingAllClasses, expectedClassCount)
var classes = [AnyObject]()
for i in 0 ..< actualClassCount {
if let currentClass: AnyClass = allClasses[Int(i)],
class_conformsToProtocol(currentClass, ContextRoutable.self) {
classes.append(currentClass)
}
}
allClasses.deallocate(capacity: Int(expectedClassCount))
return classes
}
}
I tried it in my application and it works. I clocked it in the simulator and it takes 0.05s for an application that has about 12000 classes.
Consider taking the Swift approach using a protocol instead. I think the solution is actually simpler than the Objective-C approach. There are variations of this with Self constraints which are even better if you have more control over the classes.
// define a protocol to create an instance of a class
protocol FactoryInstantiable {
static func makeFactoryInstance() -> AnyObject
}
// Factory for generating new instances
public class InstanceFactory: NSObject {
public class func instanceOf(typeName: String) -> AnyObject! {
if let ProductType = NSClassFromString(typeName) as? FactoryInstantiable.Type {
return ProductType.makeFactoryInstance()
} else {
return nil
}
}
}
// your class which generally could be defined somewhere else
class MyClass {
var counter : Int
init(counter: Int) {
self.counter = 0
}
}
// extension of your class to conform to the FactoryInstantiable protocol
extension MyClass : FactoryInstantiable {
static func makeFactoryInstance() -> AnyObject {
return MyClass(counter: 0)
}
}