1

I have successfully been able to create a singleton object in Swift, but I feel that the implementation is rather verbose. Is there a way to shorten this code up? And combine multiple formatters into one class where each formatter is its own singleton?

import Foundation

class sharedNumberFormatterWithOneDecimalPlace : NSNumberFormatter {
    class var sharedInstance: sharedNumberFormatterWithOneDecimalPlace {
    struct Singleton {
        static let instance = sharedNumberFormatterWithOneDecimalPlace()
        }
    return Singleton.instance
    }

    override init () {
        super.init()
        self.minimumIntegerDigits = 1
        self.maximumFractionDigits = 1
        self.minimumFractionDigits = 1
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

In my other class I can call it by:

NSNumberFormatter *formatter = sharedNumberFormatterWithOneDecimalPlace.sharedInstance;
NSLog(@"%@", [formatter stringFromNumber:aNumber]);

I would like to be able to have 1 class of "MultipleFormatters" where I set up many formatters that get used all over the place, and then call something like "MultipleFormatters.OneDecimalPlace" for example. PS. I have already read post: Using a dispatch_once singleton model in Swift
Thanks.

1

4 Answers 4

7

Best way in my opinion is :

private let _MultipleFormattersSharedInstance = MultipleFormatters()

class MultipleFormatters {
    class var sharedInstance: MultipleFormatters {
        return _MultipleFormattersSharedInstance
    }
}

and you can use

MultipleFormatters.sharedInstance

Then you can create all the "formatters" that you need as methods from your class MultipleFormatters and call them all over the place.

Thanks to that thing in swift

Edit for example:

Let's say you want to have a formatter called "increment". I don't know what you mean by formatters but here's a stupid example.

private let _MultipleFormattersSharedInstance = MultipleFormatters()

class MultipleFormatters {
   class var sharedInstance: MultipleFormatters {
       return _MultipleFormattersSharedInstance
    }

    func increment(number: Int, amount: Int) -> Int{
       //Maybe you need something that was initialized with
       //the singleton here.
        return number + amount
    }
}

and you would use

let singleton : MultipleFormatters = MultipleFormatters.sharedInstance
let result = singleton.increment(1, amount: 25)
Sign up to request clarification or add additional context in comments.

1 Comment

I can't fully grasp how this would work, can you make a more concrete/finished example?
1
import Cocoa
import Foundation

class MultipleFormatters : NSNumberFormatter {
    class var oneDecimalPlace: MultipleFormatters {
    struct Singleton0 {
        static let instance = MultipleFormatters(numberOfDigits: 1)

        }
        return Singleton0.instance
    }
    class var twoDecimalPlace: MultipleFormatters {
    struct Singleton {
        static let instance = MultipleFormatters(numberOfDigits: 2)

        }
        return Singleton.instance
    }

    convenience init(numberOfDigits:Int){
        self.init()
        self.maximumFractionDigits = numberOfDigits
    }

    override init () {
        super.init()
        self.minimumIntegerDigits = 1
        self.maximumFractionDigits = 1
        self.minimumFractionDigits = 1
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

var res = MultipleFormatters.oneDecimalPlace.stringFromNumber(1.234)
var asdf = MultipleFormatters.twoDecimalPlace.stringFromNumber(1.234)

Comments

0

My coworker helped out on this, this is what we think is the best way to create some formatters from one class and also be able to set options for different number formatter styles.

private let _oneDecimalPlaceInstance = MultipleFormatters(minFracDigits: 1, maxFracDigits: 1)
private let _twoDecimalPlaceInstance = MultipleFormatters(minFracDigits: 2, maxFracDigits: 2)

class MultipleFormatters : NSNumberFormatter {

class var oneDecimalPlace: MultipleFormatters {
    return _oneDecimalPlaceInstance
}

class var twoDecimalPlace: MultipleFormatters {
    return _twoDecimalPlaceInstance
}

convenience init(minFracDigits:Int, maxFracDigits:Int) {
    self.init()
    self.minimumIntegerDigits = 1
    self.minimumFractionDigits = minFracDigits
    self.maximumFractionDigits = maxFracDigits
}

}

Comments

0
class TestClass{
    private static var instance = TestClass()
    public static func shareInstance() ->TestClass{
        return TestClass.instance
    }
}

let a1 = TestClass.shareInstance()
let a2 = TestClass.shareInstance()
let a3 = TestClass()
print(a1===a2)
print(a1===a3)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.