Swift3 Swift 3.1中数据字节到sockaddr的转换

Swift3 Swift 3.1中数据字节到sockaddr的转换,swift3,unsafe-pointers,unsafemutablepointer,Swift3,Unsafe Pointers,Unsafemutablepointer,我正在将数据字节转换为sockaddr以获取sa_family\t 在ObjC中,如下所示: NSData * hostAddress; - (sa_family_t)hostAddressFamily { sa_family_t result; result = AF_UNSPEC; if ( (self.hostAddress != nil) && (self.hostAddress.length >= sizeof(struc

我正在将
数据字节
转换为
sockaddr
以获取
sa_family\t

ObjC
中,如下所示:

NSData *     hostAddress;
- (sa_family_t)hostAddressFamily {
    sa_family_t     result;

    result = AF_UNSPEC;
    if ( (self.hostAddress != nil) && (self.hostAddress.length >= sizeof(struct sockaddr)) ) {
        result = ((const struct sockaddr *) self.hostAddress.bytes)->sa_family;
    }
    return result;
}
var hostAddress:Data?

 private func hostAddressFamily() -> sa_family_t{

        var result: sa_family_t = sa_family_t(AF_UNSPEC)
        if (hostAddress != nil) && ((hostAddress?.count ?? 0) >= MemoryLayout<sockaddr>.size) {

            // Generic parameter 'ContentType' could not be inferred
            self.hostAddress!.withUnsafeBytes({ bytes in
                bytes.withMemoryRebound(to: sockaddr.self, capacity: 1, {sockBytes in
                    result = sockBytes.pointee.sa_family
                })
            })
        }

        return result
    }
在swift中,我尝试将其转换为以下内容:

NSData *     hostAddress;
- (sa_family_t)hostAddressFamily {
    sa_family_t     result;

    result = AF_UNSPEC;
    if ( (self.hostAddress != nil) && (self.hostAddress.length >= sizeof(struct sockaddr)) ) {
        result = ((const struct sockaddr *) self.hostAddress.bytes)->sa_family;
    }
    return result;
}
var hostAddress:Data?

 private func hostAddressFamily() -> sa_family_t{

        var result: sa_family_t = sa_family_t(AF_UNSPEC)
        if (hostAddress != nil) && ((hostAddress?.count ?? 0) >= MemoryLayout<sockaddr>.size) {

            // Generic parameter 'ContentType' could not be inferred
            self.hostAddress!.withUnsafeBytes({ bytes in
                bytes.withMemoryRebound(to: sockaddr.self, capacity: 1, {sockBytes in
                    result = sockBytes.pointee.sa_family
                })
            })
        }

        return result
    }
var主机地址:数据?
private func hostAddressFamily()->sau family\t{
var结果:sa_family_t=sa_family_t(AF_Unsec)
如果(主机地址!=nil)&((主机地址?.count±0)>=MemoryLayout.size){
//无法推断泛型参数“ContentType”
self.hostAddress!.withUnsafeBytes({bytes in.)
withMemoryRebound(to:sockaddr.self,容量:1,{sockBytes in
结果=sockBytes.pointee.sa_族
})
})
}
返回结果
}

获取错误:无法推断泛型参数“ContentType”

查看
数据的签名。WithUnsafebyTestType

func withUnsafeBytes<ResultType, ContentType>(_ body: (Swift.UnsafePointer<ContentType>) throws -> ResultType) rethrows -> ResultType
此外,您不太可能需要绑定内存两次,因为
NSData
是非类型化的,并且您已经指定了要绑定到的类型


总而言之:

func hostAddressFamily() -> sa_family_t {

  var result = sa_family_t(AF_UNSPEC)

  guard
    let hostAddress = hostAddress,
    hostAddress.count >= MemoryLayout<sockaddr>.size
  else {
    return result
  }

  hostAddress.withUnsafeBytes { (_ bytes: UnsafePointer<sockaddr>) in
    result = bytes.pointee.sa_family
  }

  return result
}
func hostAddressFamily()->sa_family\t{
var结果=sa_系列(AF_UNSPEC)
警卫
让hostAddress=hostAddress,
hostAddress.count>=MemoryLayout.size
否则{
返回结果
}
hostAddress.withUnsafeBytes{(bytes:UnsafePointer)位于
结果=bytes.pointee.sa_族
}
返回结果
}