Spekham2013 Spekham2013 - 1 year ago 55
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"))


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

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


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 Source

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)
    // ...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download