问题描述:

I am trying find string value from coredata entity which some time return either string value or nil value. I have to join that value with another string but when its return nil application crashed

my code snippet

let objtest = (TblObjectValue.findOrCreate(["objectId":dicCurrent.valueForJSON("Id")]) as TblObjectValue)

tablecell.lblTitle?.text = "Value: "+objtest.displayValue!

Where TblObjectValue is NSManagedObject and objectId(NSString) and displayValue(NSString) are attribute of TblObjectValue entity

application crash if displayValue is null

i want it in one line beacuse i used following code many time in my project

tablecell.lblTitle?.text = (dicCurrent.valueForJSON("CardLabel") as? String)!+": "+(TblObjectValue.findOrCreate(["objectId":dicCurrent.valueForJSON("Id")]) as TblObjectValue).displayValue!

网友答案:

Check it before do anything:

if objtest.displayValue != nil {
  tablecell.lblTitle?.text = "Value: "+objtest.displayValue!
}
网友答案:

Doesn't

tablecell.lblTitle?.text = "Value: "+ (objtest.displayValue ?? "")

do the trick?

相关阅读:
Top