Spekham2013 Spekham2013 - 3 months ago 14
iOS Question

Unwrapping a optional in swift wich is an optional but swift doesn't know its a optional

Im working on a app to convert model paint color's. I just got coreData to work and now i ran into a different problem.

let colorL: String = String(color.valueForKey("revell"))
let AmountL: String = String(color.valueForKey("filled"))

print(colorL)
print(AmountL)

cell.ColorLabel.text = colorL
cell.AmountLabel.text = AmountL


This is my code for the tableview. and my console output is this


Optional(68)
Optional(g)


and so the problem is if you haven't guessed yet that i need to unwrap a optional that cannot be unwrapped because swift doesn't know it is a optional and ik keep on getting a error that i can't unwrap a non optional.

Answer

valueForKey returns an optional (AnyObject? to be specific). Unwrap that and then build a string with it.

if let value = color.valueForKey("revell") as? /* Some Type */ {
    let amountL: String = String(value)
    // ...
}
Comments