toggle.setValue(NSNumber(value: Int(xOr[0])! as Int), forKey: "tag")
results in
Fatal error: Unexpectedly found nil while unwrapping an Optional value.
toggle.setValue(NSNumber(value: Int(xOr[0])! as Int), forKey: "tag")
results in
Fatal error: Unexpectedly found nil while unwrapping an Optional value.
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'.