【发布时间】:2019-01-15 13:28:31
【问题描述】:
我想在 Swift 3 和 4 中使用这个 Swift 2 代码:
func getLocalIP() -> String {
var address = "error"
var interfaces : UnsafeMutablePointer<ifaddrs>? = nil
var temp_addr : UnsafeMutablePointer<ifaddrs>? = nil
var success : Int32 = 0
// Retrieve the current interfaces - returns 0 on success.
success = getifaddrs(&interfaces)
if success == 0 {
// Loop through linked list of interfaces.
temp_addr = interfaces
while temp_addr != nil {
if temp_addr?.pointee.ifa_addr.pointee.sa_family == UInt8(AF_INET) {
// Check if interface is en0 which is the wifi connection on the iPhone.
if String(cString: (temp_addr?.pointee.ifa_name)!) == "en0" {
// Get NSString from C string.
address = String(cString: inet_ntoa(UnsafeMutablePointer<sockaddr_in>((temp_addr?.pointee.ifa_addr)!).pointee.sin_addr))
}
}
temp_addr = temp_addr?.pointee.ifa_next
}
}
// Free memory.
freeifaddrs(interfaces)
return address
}
UnsafeMutablePointer 出现错误:
'init' 不可用:使用'withMemoryRebound(to:Capacity:_)'
【问题讨论】:
标签: swift3 swift2 task swift4 info