On Xcode 10.1
, the following statement wasn't having any issues, but after updating the Xcode
to 10.2
compiler is generating warning for the statement.
return [
"sublevels": (self.sublevels?.array ?? [Sublevel]()) as NSObject
]
sublevels
is NSOrderedSet
, and the warning generated is as follows:
Left side of nil coalescing operator '??' has non-optional type '[Any]?', so the right side is never used
But if I break the single statement as follows, the warning disappears.
let sublevels = self.sublevels?.array ?? [Sublevel]()
return [
"sublevels": sublevels as NSObject
]
Please, will anyone explain - what is the issue with the first statement ?
As mentioned by OOPer, the solution is to provide a default Any
value as right hand side of the operator, in this case an [Any]
, because the NSOrderedSet
has no specific type bound to it. The solution is:
return [
"sublevels": (self.sublevels?.array ?? []) as NSObject
]
For more info on this matter I suggest you take a look at type casting. At the bottom of the page there is an explanation about casting the Any
type.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments