UnsafeMutablePointer to expected argument type UnsafeMutablePointer<_> in Swift 3

glenstorey

In the main.swift file, we have a call to our receipt checking system (generated by Receigen). In Swift 2, main.swift read:

startup(Process.argc, UnsafeMutablePointer<UnsafePointer<Int8>>(Process.unsafeArgv))

After upgrading to Swift 3, I've got as far as:

startup(CommandLine.argc, UnsafeMutablePointer<UnsafePointer<Int8>>(CommandLine.unsafeArgv))

which shows the error:

Cannot convert value of type UnsafeMutablePointer<UnsafeMutablePointer<Int8>?> (aka UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) to expected argument type UnsafeMutablePointer<_>

Update: Using the linked question so that it reads:

startup(CommandLine.argc, UnsafeMutableRawPointer(CommandLine.unsafeArgv)
    .bindMemory(
        to: UnsafeMutablePointer<Int8>.self,
        capacity: Int(CommandLine.argc)))

Produces:

Cannot convert value of type UnsafeMutablePointer<Int8>.Type to expected argument type UnsafePointer<Int8>?.Type (aka Optional<UnsafePointer<Int8>>.Type)

Where the compiler is referring to the to:UnsafeMutablePointer.

The header for startup looks like:

int startup(int argc, const char * argv[]);

How can I successfully pass the variables to startup in main.swift?

matt

Basically, this is a variant on the problem discussed here:

Xcode 8 beta 6: main.swift won't compile

The problem is that you have an impedance mismatch between the type of CommandLine.unsafeArgv and the type expected by your C function. And you can no longer cast away this mismatch merely by coercing from one mutable pointer type to another. Instead, you have to pivot (as it were) from one type to another by calling bindMemory. And the error message, demanding a Optional<UnsafePointer<Int8>>.Type, tells you what type to pivot to:

    startup(
        CommandLine.argc,
        UnsafeMutableRawPointer(CommandLine.unsafeArgv)
            .bindMemory(
                to: Optional<UnsafePointer<Int8>>.self,
                capacity: Int(CommandLine.argc))
    )

That should allow you to compile. Testing on my machine with a stub of startup, it actually runs. But whether it will run on your machine, and whether it is safe, is anybody's guess! This stuff is undeniably maddening...

EDIT The problem with CommandLine.unsafeArgv is fixed in iOS 12 / Xcode 10, so it may be that this problem is fixed too.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Swift: Cannot convert value of type 'UnsafeMutablePointer' to expected argument type 'UnsafeMutablePointer'

Cannot convert value of type 'SecCertificate' to expected argument type 'UnsafeMutablePointer<UnsafeRawPointer?>!'

Casting a variable of type 'UnsafeMutableRawPointer' to UnsafeMutablePointer<> in Swift 3

UnsafeMutablePointer<CFTypeRef> in Swift 3

swift Cannot convert value of type 'in_addr_t' (aka 'UInt32') to expected argument type 'UnsafeMutablePointer<in_addr_t>!'

Swift error: '&' used with non-inout argument of type 'UnsafeMutablePointer'

Error in UnsafeMutablePointer in swift3

How to use UnsafeMutablePointer in Swift 3?

UnsafeMutablePointer<Bytef> swift3

Cannot convert value of type 'UnsafePointer<UnsafeMutablePointer<Float>>?' to expected argument type 'UnsafePointer<Float>?'

How to create UnsafeMutablePointer<UnsafeMutablePointer> in swift

Swift UnsafeMutablePointer & UnsafeMutablePointer<UnsafePointer<SomeType>>

How to cast self to UnsafeMutablePointer<Void> type in swift

Cannot assign value of type UnsafeMutablePointer ObjCBool in Swift

Convert UnsafeMutableRawPointer to UnsafeMutablePointer<T> in swift 3

How to use UnsafeMutablePointer<T?> in swift 3

UnsafeMutablePointer as an output argument in PyObjC

Why when converting Float to CGFloat Xcode give me a warning "Cannot convert value of type 'CGFloat' to expected argument type 'UnsafeMutablePointer"?

Typecast UnsafeMutablePointer<Void> to UnsafeMutablePointer<#Struct type#>

'map' produces [T], not the expected contextual result type 'UnsafeMutablePointer<CGFloat>'

Function that expects an UnsafeMutablePointer<Type>!

How to convert a type of "any" in kind "UnsafeMutablePointer<Int16>" in a swift 3

Cannot invoke 'enumerateObjects' with an argument list of type '((AnyObject!, NSInteger, UnsafeMutablePointer<ObjCBool>) -> ())'

How to do pointer arithmetic on UnsafeMutablePointer and get String in Swift 3

How to convert String to UnsafeMutablePointer<UInt8> ? Swift 3

Swift 2 - UnsafeMutablePointer<Void> to object

Using UnsafeMutablePointer and CFRunLoopObserverContext in swift 2

Cast a Swift struct to UnsafeMutablePointer<Void>

What is UnsafeMutablePointer<Unmanaged<CMSampleBuffer>?> in Swift?