3
我需要你的幫助,這個代碼不會再編譯斯威夫特3斯威夫特3 - 轉換C語言結構SOCKADDR_IN到CFData
//: Let's set up the `sockaddr_in` C structure using the initializer.
var sin = sockaddr_in(
sin_len: UInt8(sizeof(sockaddr_in)),
sin_family: sa_family_t(AF_INET),
sin_port: in_port_t(0),
sin_addr: in_addr(s_addr: inet_addr(routerIP)),
sin_zero: (0,0,0,0,0,0,0,0)
)
//: Now convert the structure into a `CFData` object.
let data = withUnsafePointer(&sin) { ptr in
CFDataCreate(kCFAllocatorDefault, UnsafePointer(ptr), sizeof(sockaddr_in))
}
//: Create the `CFHostRef` with the `CFData` object and store the retained value for later use.
host = CFHostCreateWithAddress(kCFAllocatorDefault, data).takeRetainedValue()
我必須要修好行「withUnsafePointer」但我不知道怎麼樣?
我想是這樣的
let data = withUnsafePointer(to: &sin) { ptr in
CFDataCreate(kCFAllocatorDefault, UnsafePointer(ptr), MemoryLayout<sockaddr_in>.size)
}
編譯說我必須使用withMemoryRebound但如何? 感謝
感謝珍妮,現在卻編譯我有一個新錯誤 –
我在這裏發佈了我的新錯誤,http://stackoverflow.com/questions/39476628/swift-3-cfhostschedulewithrunloop-crash –