[swift-users] Updating C-wrapper to Swift 3
John Brownie
john_brownie at sil.org
Wed Sep 28 12:03:25 CDT 2016
Thanks for the pointers. Good reading, but I'm still confused. I think
that the first issue is clear to me now, but I don't know what I have to
do to make the closure be seen as satisfying the type of the function
pointer. It worked in Swift 2.2 as
XML_SetEndElementHandler(parser) { (userData:
UnsafeMutablePointer<Void>, name: UnsafePointer<XML_Char>) -> Void in
All that's changed is making the UnsafeMutablePointer<Void> into
UnsafeMutableRawPointer, unless it's the unwrapped nature of the
parameter in the error message:
Cannot convert value of type '(UnsafeMutableRawPointer,
UnsafePointer<XML_Char>) -> Void' to expected argument type
'XML_EndElementHandler!'
I don't know what I need to do next. Any help is much appreciated.
> Quinn "The Eskimo!" via swift-users <mailto:swift-users at swift.org>
> 28 September 2016 at 11:26
>
> I recommend reading the whole “Migrating to Swift 2.3 or Swift 3 from
> Swift 2.2” doc, but for this specific issue you should start with the
> “UnsafeRawPointer Migration” doc that it links to.
>
> <https://swift.org/migration-guide/>
>
> <https://swift.org/migration-guide/se-0107-migrate.html>
>
> Share and Enjoy
> --
> Quinn "The Eskimo!" <http://www.apple.com/developer/>
> Apple Developer Relations, Developer Technical Support, Core OS/Hardware
>
>
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
> John Brownie <mailto:john_brownie at sil.org>
> 28 September 2016 at 9:29
> I have a Swift wrapper for the expat XML parser, and I just went
> through the update to Swift 3, and I'm thrown a series of errors that
> mostly appear to be of two types. Here is a representative function of
> the first type:
>
> func parse() throws {
> var done = false
> while !done {
> let buffer: UnsafeMutablePointer<Void> =
> XML_GetBuffer(parser, Int32(bufsize))
> if buffer == nil {
> let errorCode = XML_GetErrorCode(parser)
> throw ExpatError(reason: "Error \(errorCode),
> \(XML_ErrorString(errorCode))")
> }
> let bytesRead =
> readData(UnsafeMutablePointer<UInt8>(buffer), bufferLength: bufsize)
> done = bytesRead < bufsize
> if XML_ParseBuffer(parser, Int32(bytesRead), Int32(done ?
> 1 : 0)) != XML_STATUS_OK {
> let errorCode = XML_GetErrorCode(parser)
> throw ExpatError(reason: "Error \(errorCode),
> \(XML_ErrorString(errorCode))")
> }
> }
> }
>
> Xcode 8's migrator changed the declaration of buffer to
> UnsafeMutableRawPointer?, but the readData call gives an error:
>
> Cannot invoke initializer for type 'UnsafeMutablePointer<UInt8>' with
> an argument list of type '(UnsafeMutableRawPointer?)'
> Pointer conversion restricted: use '.assumingMemoryBound(to:)' or
> '.bindMemory(to:capacity:)' to view memory as a type.
> Overloads for 'UnsafeMutablePointer<UInt8>' exist with these partially
> matching parameter lists: (RawPointer), (OpaquePointer),
> (OpaquePointer?), (UnsafeMutablePointer<Pointee>),
> (UnsafeMutablePointer<Pointee>?)
>
> Where can I read up on how to make the conversion? My initial effort is:
>
> let bytesRead = readData(buffer!.bindMemory(to:
> UInt8.self, capacity: bufsize), bufferLength: bufsize)
>
> That's more or less thrashing around with no real understanding.
>
> The second type is turning a closure into the appropriate C function
> pointer. For example:
>
> XML_SetEndElementHandler(parser) { (userData:
> UnsafeMutableRawPointer, name: UnsafePointer<XML_Char>) -> Void in
> let theParser = unsafeBitCast(userData, to:
> ExpatSwift.self)
> let theName = ExpatSwift.getString(name)
> theParser.endElement(theName)
> }
>
> This gets an error:
>
> Cannot convert value of type '(UnsafeMutableRawPointer,
> UnsafePointer<XML_Char>) -> Void' to expected argument type
> 'XML_EndElementHandler!'
>
> XML_EndElementHandler is defined as:
>
> typedef void (XMLCALL *XML_EndElementHandler) (void *userData,
> const XML_Char *name);
>
> Here I have no real clue as to how to fix it.
>
> So, any pointers to either how to fix these problems or to a good
> source to read to understand them?
>
> John
--
John Brownie
In Finland on furlough from SIL Papua New Guinea
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160928/4de67acc/attachment.html>
More information about the swift-users
mailing list