Ozvengrad
Ozvengrad

Reputation: 302

Multidimensional Array Unwrapping an Optional

so I'm having this problem where I can't unwrap an optional for outputting it into the label, I've even tried just printing it in the console and it still gives me an optional

The code and array are in different files.

Code It's in a VC:

for var i = 0; i < stateName.count; i++ {
        if tax.state == stateName[i][0] {
            stateName[i][1] = Double(taxNumb.text!)!
            print(stateName[i][1])
            output.text = String(stateName[i][1])
        }
    }

Array Code I made this in an empty swift file:

var tax : Taxes? = nil

var stateName = [
  ["AK - Alaska", tax?.alaska!],
  ["AL - Alabama", tax?.alabama!],
  ["AR - Arkansas", tax?.arkansas!],
  ["AZ - Arizona", tax?.arizona!],
  ["CA - California", tax?.california!]
]

Upvotes: 0

Views: 145

Answers (1)

luk2302
luk2302

Reputation: 57174

As I wrote in my comment to your previous question use the "Nil Coalescing" ?? operator:

output.text = String(stateName[i][1] ?? "not set")

Or using the alternate swift String magic

output.text = "\(stateName[i][1] ?? "not set")"

The operator returns the first value if it not nil, otherwise it returns the second value.

Upvotes: 1

Related Questions