[swift-users] IUO from C Library Interface

Joe Groff jgroff at apple.com
Mon Oct 24 16:32:30 CDT 2016


> On Oct 24, 2016, at 2:24 PM, Ryan Lovelett via swift-users <swift-users at swift.org> wrote:
> 
> import CommonCrypto
> 
> protocol Foo {
>  associatedtype Context
>  var context: Context { get set }
>  var bar: (UnsafeMutablePointer<Context>!, UnsafeRawPointer!, CC_LONG)
>  -> Int32 { get }
> }
> 
> struct SHA1: Foo {
>  var context: CC_SHA1_CTX
>  var bar: (UnsafeMutablePointer<CC_SHA1_CTX>!, UnsafeRawPointer!,
>  CC_LONG) -> Int32 = CC_SHA1_Update
> }
> 
> Unfortunately this will not compile any longer with Swift 3.0. The error
> is: Implicitly unwrapped optionals are only allowed at top level and as
> function results.
> 
> I can try modifying `bar` definition to be `?` instead of `!` but then
> `CC_SHA1_Update`) can no longer be assigned to `bar`.
> 
> Suggestions?

Not being able to assign the function reference is a bug. As a workaround, you should be able to unsafeBitCast CC_SHA1_Update to the appropriate type.

-Joe


More information about the swift-users mailing list