It seems that Xcode 9.3 does fix one issue I was having, but in Swift 4.1 the second half of this code still doesn't compile:
var obj: SomeClass! ; class SomeClass {}
func inoutFunc(_: inout SomeClass?) {}
inoutFunc(&obj) // works
func pointerFunc(_: UnsafeMutablePointer<SomeClass?>) {}
pointerFunc(&obj) // <-- COMPILER ERROR
The call to inoutFunc
is now fine, but the call to pointerFunc
still gives me an error:
Cannot invoke 'pointerFunc' with an argument list of type '(inout SomeClass!)'
Or in the original context:
Cannot pass immutable value of type 'ActualClass?' as inout argument
Similar to my Swift 4.0 issue (where the inoutFunc
didn't compile either) if I change the declaration to var obj: SomeClass?
then the second function call compiles without complaint.
Is this another lingering Swift bug related to Implicitly Unwrapped Optionals, or would this UnsafeMutablePointer
situation not be expected to work like the inout
version now does? Is there a relatively clean workaround?
Background:
In the actual code, the pointerFunc
call is an Apple framework function which either initializes the instance or returns an error status.
Since I already guard AppleFrameworkInitializer(&obj) == noErr else { /* … */ }
, I don't want to deal with re-assigning from a temporary optional, or have to constantly unwrap obj!
in all the code that follows.
That is, this seems like a legitimate use case for Implicitly Unwrapped Optionals and I'm wondering why I still can't use one here.