I have a the following code which i am trying to use to initialize a variable and perform some operation on it.
let formattedPointsValue: String?
self.formattedPointsValue = model.pointUnitsEarned.stringValueWithWhiteSpaceThousandSeperator()+" "+"model.name".localized(in: .name) ?? .none
However i am getting the warning
Left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used.
When i remove the ?? .none
my projects runs fine without a problem however when i run my unit tests i get an error
fatal error: unexpectedly found nil while unwrapping an Optional value
The only way i found to solve this is to use this code.
if let unformattedValue = model.pointUnitsEarned {
self.formattedPointsValue = unformattedValue.stringValueWithWhiteSpaceThousandSeperator()+" "+"model.name".localized(in: .name)
} else {
self.formattedPointsValue = nil
}
I would like to understand why something like this works:
let legend: String?
self.legend = model.pointsCategory ?? .none
but this fails:
let formattedPointsValue: String?
self.formattedPointsValue = model.pointUnitsEarned.stringValueWithWhiteSpaceThousandSeperator()+" "+"model.name".localized(in: .name) ?? .none