问题
I found this bug when tried to get ip address through wifi. After install fabric, i got this error
0 MY_APP 0x1020e538c specialized FixedWidthInteger.init<A>(_:radix:)
(My_Screen.swift)
After find the method error, i found this line :
if addrFamily == UInt8(AF_INET) || addrFamily == UInt8(AF_INET6) linked to FixedWidthInteger:
public struct UInt8 : FixedWidthInteger, UnsignedInteger
I dont know why this problem triggered. can some one help?
Here i my full code to get currentIp address
func getIPAddress() -> String? {
var address: String?
var ifaddr: UnsafeMutablePointer<ifaddrs>? = nil
if getifaddrs(&ifaddr) == 0 {
var ptr = ifaddr
while ptr != nil {
defer { ptr = ptr?.pointee.ifa_next }
let interface = ptr?.pointee
let addrFamily = interface?.ifa_addr.pointee.sa_family
if addrFamily == UInt8(AF_INET) || addrFamily == UInt8(AF_INET6) {
if let name = String(cString: (interface?.ifa_name)!) as String?, name == "en0" {
var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
getnameinfo(interface?.ifa_addr, socklen_t((interface?.ifa_addr.pointee.sa_len)!), &hostname, socklen_t(hostname.count), nil, socklen_t(0), NI_NUMERICHOST)
address = String(cString: hostname)
}
}
}
freeifaddrs(ifaddr)
}
return address
}
UPDATE NOTE: I have 4 devices ios 12.1 + 1 device ios 12.2, only 1 device ios 12.1 + ios device 12.2 crash, 3 other devices work normally.
来源:https://stackoverflow.com/questions/55491986/swift-crash-when-getcurrentip-address-fixedwidthinteger-inita