0

This is my first time implementing a singleton to share an instance of an object in swift. Everything seems to be working totally fine, except for when I try and add an element to an array (accessing from another class) that lives within my singleton object. It indeed does not append any objects to the array at all. I'm thinking that it's appending onto an array, but not the same instance of the class that I would like it to be (as I only want one and only one instance). However, If I append elements onto the array from the init() of the class, everything works out just fine. Here's some code (I've simplified all the classes to make things more obvious):

File 1:

class Brew: NSObject {
  var method = Method()

  //Singleton variable
  private static var currentBrew: Brew?

  //Method to get the current (and only) brew object
  static func getCurrentBrew() -> Brew {
    if currentBrew == nil {
        currentBrew = Brew()
    }
    return currentBrew!
  }

}

struct Method {
  var chemex = Device()

init() {
  //If I append here - everything works fine
  //chemex.instructions.append = (Instruction(title: "Prepare", direction: "Prewet & Heat", time: 3, water: 0))
}

}

struct Device {
  var instructions = [Instruction]() 

init() {
    instructions.append(Instruction(title: "None", direction: "None", time: 1, water: 0, index: 0)) 
}

File 2: (where I would like to append to the array of instructions)

let brew = Brew.getCurrentBrew() //How i'm accessing the object

//I'm calling this method from viewDidLoad to set up the array
func setupBrewDevices() {
  //This is the line that does not actually append to the singleton instance
  brew.method.chemex.instructions.append(Instruction(title: "Extraction", direction: "Match water.", time: 8 , water: 25))

Just a side note, I also tried to make a method that would append an instruction onto the array that lives inside of the same class, but that had the same result. Hopefully this is clear enough - I appreciate any help!

Thanks, Cole

1 Answer 1

1

There is a better way to create a singleton instance in Swift.

class Brew: NSObject {
    static let currentBrew = Brew()

    var method = Method()
}

This is thread-safe and avoids using an optional.

That said, when I tried your code the instructions array ended up with two elements like I would expect ("None") and ("Extraction"). The problem may lie elsewhere in your code.

Sign up to request clarification or add additional context in comments.

2 Comments

Adding private init() {} does prevent someone from initializing multiple instances of Brew. I would also remove the :NSObject part.
Great, thank you for this. I'm still working on a solution, but it's good to eliminate half the mess.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.