-3
toggle.setValue(NSNumber(value: Int(xOr[0])! as Int), forKey: "tag")

results in

Fatal error: Unexpectedly found nil while unwrapping an Optional value.

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
  • Unrelated but never bridge cast a type to the same type. `Int(...)! as Int` is pretty nonsensical. – vadian Oct 15 '20 at 06:54

1 Answers1

0

The element in xOr at index 0 is either not present (if it is a dictionary) or nil (if it is an array).

You should add a check before the element:

if xOr.count > 0, let value = xOr[0] as? Int // Maybe remove the count, I don't know whether this is an array or a dictionary. If it is an array, keep the check, if it is a dictionary, keep it.
{
  toggle.setValue(NSNumber(value: value), forKey: "tag")
}

Also, Int(xOr[0])! already (forcefully) unwraps the value, so you do not need an additional cast to 'Int'.

inexcitus
  • 2,471
  • 2
  • 26
  • 41