使用Swift从NSTextfield到(常量无符号字符*)

使用Swift从NSTextfield到(常量无符号字符*),swift,string,nstextfield,unsafe-pointers,Swift,String,Nstextfield,Unsafe Pointers,如何在C程序中正确地从Swift中的NSTextField接收(const unsigned char*) 案例1: let string1 = atextfield!.stringValue // -> "Test" print(string1) // -> "Test" 调试中的string1/\u core/\u baseAddress=nil// let string2 = string1.utf8CString.withUnsafeBytes { ptr ->

如何在C程序中正确地从Swift中的NSTextField接收(const unsigned char*)

案例1

let string1 = atextfield!.stringValue // -> "Test"
print(string1) // -> "Test"
调试中的string1/\u core/\u baseAddress=nil//

let string2 = string1.utf8CString.withUnsafeBytes
{
    ptr -> UnsafePointer<UInt8> in
    return ptr.load(as: UnsafePointer<UInt8>.self)
}
let string2 :UnsafePointer<UInt8> = string1.utf8CString.withUnsafeBufferPointer {
($0.baseAddress!.withMemoryRebound(to: UnsafePointer<UInt8>.self, capacity: string1.lengthOfBytes(using: .utf8)) {$0})}.pointee

testCPgm(string2)
string1/_core/_baseAddress=(_rawValue=0x…“Test”)//正在调试中

let string2 = string1.utf8CString.withUnsafeBytes
{
    ptr -> UnsafePointer<UInt8> in
    return ptr.load(as: UnsafePointer<UInt8>.self)
}
let string2 :UnsafePointer<UInt8> = string1.utf8CString.withUnsafeBufferPointer {
($0.baseAddress!.withMemoryRebound(to: UnsafePointer<UInt8>.self, capacity: string1.lengthOfBytes(using: .utf8)) {$0})}.pointee

testCPgm(string2)
致命错误:UnsafeRawBufferPointer.load超出范围

案例3

let string1 = "Test"
print(string1) // -> "Test"
let string1 = atextfield!.stringValue // Value = "Test"
print(string1) // -> "Test"
let string1 = "Test"
print(string1) // -> "Test"
调试中的string1/\u core/\u baseAddress=nil//

let string2 = string1.utf8CString.withUnsafeBytes
{
    ptr -> UnsafePointer<UInt8> in
    return ptr.load(as: UnsafePointer<UInt8>.self)
}
let string2 :UnsafePointer<UInt8> = string1.utf8CString.withUnsafeBufferPointer {
($0.baseAddress!.withMemoryRebound(to: UnsafePointer<UInt8>.self, capacity: string1.lengthOfBytes(using: .utf8)) {$0})}.pointee

testCPgm(string2)
案例4

let string1 = "Test"
print(string1) // -> "Test"
let string1 = atextfield!.stringValue // Value = "Test"
print(string1) // -> "Test"
let string1 = "Test"
print(string1) // -> "Test"
string1/_core/_baseAddress=(_rawValue=0x…“Test”)//正在调试中

let string2 = string1.utf8CString.withUnsafeBytes
{
    ptr -> UnsafePointer<UInt8> in
    return ptr.load(as: UnsafePointer<UInt8>.self)
}
let string2 :UnsafePointer<UInt8> = string1.utf8CString.withUnsafeBufferPointer {
($0.baseAddress!.withMemoryRebound(to: UnsafePointer<UInt8>.self, capacity: string1.lengthOfBytes(using: .utf8)) {$0})}.pointee

testCPgm(string2)
testCPgm中的testcfield=“”

void testCPgm(const unsigned char *testcfield)
{

}
案例5-->有效

let string1 = "Test"

or

let string1 = atextfield!.stringValue
print(string1) // -> "Test"

testCPgm(string1) // Yes directly from String to (const unsigned char *)
testCPgm中的testcfield=“Test”

void testCPgm(const unsigned char *testcfield)
{

}

谢谢你的回答

这是我没有结束就找到的

从字符串(string1)到(常量无符号字符*)(string2)

我不知道是否有更优雅的方法

我没有找到数据的等价项(没有闭包)(不是NSData(use.bytes))