我还没有找到如何将以下行(错误)调整为 Swift 4 的解决方案。关于文档,应该使用 withUnsafePointer 方法,但我找不到将 addressBytes.bytes 添加到代码中的方法。
func netServiceDidResolveAddress(_ sender: NetService) {
//service.addresses - array containing NSData objects, each of which contains an appropriate
//sockaddr structure that you can use to connect to the socket
for addressBytes in sender.addresses!
{
var inetAddress : sockaddr_in!
var inetAddress6 : sockaddr_in6?
//NSData’s bytes returns a read-only pointer (const void *) to the receiver’s contents.
//var bytes: UnsafePointer<()> { get }
let inetAddressPointer = UnsafePointer<sockaddr_in>(addressBytes.bytes)
//Access the underlying raw memory
inetAddress = inetAddressPointer.memory
if inetAddress.sin_family == __uint8_t(AF_INET) //Note: explicit convertion (var AF_INET: Int32 { get } /* internetwork: UDP, TCP, etc. */)
{
}
else
{
if inetAddress.sin_family == __uint8_t(AF_INET6) //var AF_INET6: Int32 { get } /* IPv6 */
{
let inetAddressPointer6 = UnsafePointer<sockaddr_in6>(addressBytes.bytes)
inetAddress6 = inetAddressPointer6.memory
inetAddress = nil
}
else
{
inetAddress = nil
}
}
var ipString : UnsafePointer<Int8>?
//static func alloc(num: Int) -> UnsafeMutablePointer<T>
let ipStringBuffer = UnsafeMutablePointer<Int8>.allocate(capacity: Int(INET6_ADDRSTRLEN))
if (inetAddress != nil)
{