swift2 “init”不可用:使用'withMemoryRebound(to:容量:_)'不安全可变指针

bakd9h0s  于 2022-11-06  发布在  Swift
关注(0)|答案(1)|浏览(145)

我想在Swift 3和Swift 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”不可用:使用'with内存反弹(到:容量:_)'

y1aodyip

y1aodyip1#

错误文本准确地说明了错误所在以及您应该执行的操作:您不能再使用指向另一个类型指针来创建UnsafeMutablePointer,而必须使用.withMemoryRebound(to:Capacity:_)方法。
在您的情况下,您应该将

String(cString: inet_ntoa(UnsafeMutablePointer<sockaddr_in>((temp_addr?.pointee.ifa_addr)!).pointee.sin_addr))

就像这样:

temp_addr.pointee.ifa_addr.withMemoryRebound(to: sockaddr_in.self, capacity: 1, { sockaddr_ptr -> String? in
   guard let sin_addr = inet_ntoa(sockaddr_ptr.pointee.sin_addr) else { return nil }
   return String(cString: sin_addr)
})

或者,这里是函数的完全重写和增强版本:

enum LocalIPError: Error {
    case failedToRetrieveInterfaces(errorCode: Int32)
    case noEn0Address
}

func getLocalIP() throws -> String {
    // Retrieve the current interfaces - returns 0 on success.
    var interfaces : UnsafeMutablePointer<ifaddrs>? = nil
    let errorCode = getifaddrs(&interfaces)

    defer {
        // Free memory.
        freeifaddrs(interfaces)
    }

    guard
        errorCode == 0,
        let interfaceSequence = interfaces.flatMap({ first_addr in
            sequence(
                first: first_addr,
                next: { addr in addr.pointee.ifa_next }
            )
        })
        else { throw LocalIPError.failedToRetrieveInterfaces(errorCode: errorCode) }

    if let address = interfaceSequence.compactMap({ (temp_addr) -> String? in
        guard
            let ifa_addr = temp_addr.pointee.ifa_addr,
            ifa_addr.pointee.sa_family == sa_family_t(AF_INET),
            // Check if interface is en0 which is the wifi connection on the iPhone.
            String(cString: (temp_addr.pointee.ifa_name)) == "en0"
            else { return nil }

        return ifa_addr.withMemoryRebound(to: sockaddr_in.self, capacity: 1, { sockaddr_ptr -> String? in
            guard let sin_addr = inet_ntoa(sockaddr_ptr.pointee.sin_addr) else { return nil }
            return String(cString: sin_addr)
        })
    }).last {
        return address
    } else {
        throw LocalIPError.noEn0Address
    }
}

相关问题