I cannot prove what I'm going to say, so any feedback is very well appreciated .
OP claims the code is similar to this:
var type: String? = "milk" let certainType = type ?? "melon" println("it a \(certainType)")
prints an unexpected line:
"it a Optional (" milk ")"
whereas it should be:
"this is milk"
Turns out this happens when a variable is actually a property with the @NSManaged attribute.
I suspect there is an error in type inference. OP indicates that:
let certainType = type ?? "melon"
prints an incorrect result, whereas:
let certainType: String = type ?? "melon"
prints correct.
Therefore, for some reason, without explicitly specifying the type of the variable, the nil join operator returns optional.
Should I change the type of the variable type to AnyObject or AnyObject? it really prints an unexpected result :
var type: AnyObject = "milk" let certainType = type ?? "melon" println("it a \(certainType)")
"this is optional (milk)"
My guess is this: since the @NSManaged attribute is @NSManaged , the property is displayed with the wrong type ( AnyObject? ) When using it, unless the correct type is explicitly specified.
As to why this happens, there is no idea (besides the fact that this is a mistake)
Feel free to raise or decline this answer, and most importantly, do not consider it as a solution. I would be very grateful for the feedback, because I am interested in what is happening and whether this is really a mistake or not.