代码之家  ›  专栏  ›  技术社区  ›  lf_araujo

“UnsafePointer”没有成员“toString”

  •  1
  • lf_araujo  · 技术社区  · 7 年前

    我正在尝试使用 SwiftGtk

    /home/luis/Dropbox/Documentos/Coding/Swift/SwiftGtk/.build/checkouts/SwiftGtk--2571330441700638662/Sources/HeaderBar.swift:15:63: error: value of type 'UnsafePointer<gchar>' (aka 'UnsafePointer<Int8>') has no member 'toString'
            get { return gtk_header_bar_get_title(castedPointer())?.toString() }
                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^ ~~~~~~~~
    /home/luis/Dropbox/Documentos/Coding/Swift/SwiftGtk/.build/checkouts/SwiftGtk--2571330441700638662/Sources/HeaderBar.swift:20:66: error: value of type 'UnsafePointer<gchar>' (aka 'UnsafePointer<Int8>') has no member 'toString'
            get { return gtk_header_bar_get_subtitle(castedPointer())?.toString() }
                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^ ~~~~~~~~
    /home/luis/Dropbox/Documentos/Coding/Swift/SwiftGtk/.build/checkouts/SwiftGtk--2571330441700638662/Sources/HeaderBar.swift:41:75: error: value of type 'UnsafePointer<gchar>' (aka 'UnsafePointer<Int8>') has no member 'toString'
            get { return gtk_header_bar_get_decoration_layout(castedPointer())?.toString() }
    

    似乎发生的是 Gtk.HeaderBar get_title()方法返回gchar,它(我认为)属于UnsafePointer类型,在代码中未正确转换为字符串:

    public var title: String? {
        get { return gtk_header_bar_get_title(castedPointer())?.toString() }
        set { gtk_header_bar_set_title(castedPointer(), newValue) }
    }
    
    public var subtitle: String? {
        get { return gtk_header_bar_get_subtitle(castedPointer())?.toString() }
        set { gtk_header_bar_set_subtitle(castedPointer(), newValue) }
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Anatoli P    7 年前

    String 从…起 UnsafePointer<Int8> . 请看 文档位于 https://developer.apple.com/documentation/swift/string . 这里的复杂性在于,根据所使用的编码,同一字符串具有不同的字节序列表示。一种方法是使用 init(cString:encoding:) . 例如,如果您知道 非安全指针(<Int8> buf

    let s = String.init(cString: buf, encoding: String.Encoding.ascii)