0

I have this code:

var activeCalculators = Calculators()

func numberOfCalculators()->Int{
        let activeCalculatorsArray = activeCalculators.calculators
        return activeCalculatorsArray!.count
    }


struct Calculators
{
    var calculators: [Calculator]?
    var activeCalculator: Int = -1
    var activeSummary: Bool = false
    var activeProfits: Bool = false

    public func getCalculators()-> [Calculator]
    {
        return calculators!
    }

    public mutating func setCalculators(calculators: [Calculator])
    {
        self.calculators = calculators
    }

    public func  getActiveCalculatorIndex()-> Int
    {
        return activeCalculator
    }

    public mutating func  setActiveCalculatorIndex( activeCalculator: Int)
    {
        self.activeCalculator = activeCalculator
    }

    public func  isActiveProfits()-> Bool
    {
        return activeProfits
    }

    public mutating func  setActiveProfits( activeProfits: Bool)
    {
        self.activeProfits = activeProfits
    }



    public func  isActiveSummary()-> Bool
    {
        return activeSummary
    }

    public mutating func setActiveSummary(activeSummary: Bool)
    {
        self.activeSummary = activeSummary
    }
}

override func viewDidLoad() {
print("XXXXXXXXXXXX: \(numberOfCalculators())")

}

When i run my function: numberOfCalculators() i have error: Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

I'm trying add this func to my class:

public func countCalculators()->Int{
        return (calculators?.count)!
    }

but them I have error:

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

How can you fix it?

5
  • you never called setCalculators that is why it is nill Commented Jul 26, 2018 at 6:12
  • Do you have a Java background? Commented Jul 26, 2018 at 6:14
  • I would strongly recommend against using -1 as a default or sentinel value. Swift has optionals, Int? can be nil. Take advantage of that. It'll keep you from accidentally indexing into an array with -1 and crashing your program. Commented Jul 26, 2018 at 6:17
  • 1
    You do not need, and should not write, manual getter and setter functions to wrap your stored properties. Stored properties already synthesize a backing instance variable, and the getter/setter functions that are implicitly used anytime you read/write to it. stackoverflow.com/a/34006746/3141234 Commented Jul 26, 2018 at 6:19
  • 1
    Generally, it's simpler to use an empty array over a nil optional array. Does that fit your data model well? Commented Jul 26, 2018 at 6:19

2 Answers 2

1

First thing, unwrap it properly :

func numberOfCalculators()->Int{
    return activeCalculatorsArray?.count ?? 0
}

public func countCalculators()->Int {
    return calculators?.count ?? 0 // Suggested by Alexander
}

Then you need to set something before calling get. So in viewDidLoad instead of calling numberOfCalculators() call setCalculators() first so that you will have some value for calculator.

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

1 Comment

calculators?.count ?? 0
0

Based on the information you provided it seems that perhaps calculators is nil at the time of your reading. You may want to do some checks it see in your getCalculators method...

guard let calcs = self.calculators else {
    print("No calculcators!")
    return [] // returns empty (no crash)
}

return calcs // return values

In the meantime, it would be nice to investigate why you don't have any calculators in the first place.

Comments

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.