[swift-users] Problems - hard to describe

Kenny Leung kenny_leung at pobox.com
Fri Jun 24 11:47:48 CDT 2016


Hi All.

My problems are so weird that I don’t even know what to put in the subject line.

I’m using the latest Swift 3 snapshot, and I’m trying to write a bridge from [String] to (const char * const *)

Here’s my code:

public struct ArrayBridge<SwiftType,CType> {

    let originals  :[SwiftType]
    let translated :[CType]
    //let pointers   :[UnsafePointer<CType>?]

    init(array :[SwiftType], transform: @noescape (SwiftType) throws -> CType) throws {
        self.originals = array
        self.translated = try array.map(transform)
        let pointerArray = self.translated.map {
            guard let result = UnsafePointer<CType>($0) else {
                fatalError()
            }
            return result
        }
    }
}

This gives the error:

    Expression type '[_]' is ambiguous without more context
or Ambigious reference to member ‘map'

on the line “let pointerArray = self.translated.map {"

Some digging says that this may be helped if I explicitly specify the return type for the closure:

public struct ArrayBridge<SwiftType,CType> {

    let originals  :[SwiftType]
    let translated :[CType]
    //let pointers   :[UnsafePointer<CType>?]

    init(array :[SwiftType], transform: @noescape (SwiftType) throws -> CType) throws {
        self.originals = array
        self.translated = try array.map(transform)
        let pointerArray = self.translated.map { item -> UnsafePointer<CType> in
            guard let result = UnsafePointer<CType>(item) else {
                fatalError()
            }
            return result
        }
    }
}

This gives the error:

    Cannot invoke initializer for type 'UnsafePointer<CType>' with an argument list of type '((CType))’ 

on the line “ guard let result = UnsafePointer<CType>(item) else {“


Can anyone shed some light on this? What is a type with parentheses around it? I’ve also gotten errors like, “argument (_) -> _ does not match _ -> _”

Thanks!

-Kenny




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160624/e5c0cdf5/attachment.html>


More information about the swift-users mailing list