参数标签'(count:,repeatedValue :)'不匹配任何可用的重载

时间:2017-03-03 11:20:43

标签: ios swift swift3

我将Swift代码从版本2迁移到3并且存在此问题:

  

参数标签'(count:,repeatedValue :)'不匹配任何可用的重载

我的代码

static func getWiFiAddress() -> 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) {
                let name = String(cString: (interface?.ifa_name)!)
                var addr = interface?.ifa_addr.pointee
                // issue while assigning to hostname variable
                var hostname = [CChar](count: Int(NI_MAXHOST), repeatedValue: 0)
                getnameinfo(&addr, socklen_t(interface.ifa_addr.memory.sa_len),
                                &hostname, socklen_t(hostname.count),
                                nil, socklen_t(0), NI_NUMERICHOST)
                address = String.fromCString(hostname)
            }
        }
        freeifaddrs(ifaddr)
    }
    if address == nil {
        address = ""
    }
    return address
}

1 个答案:

答案 0 :(得分:5)

在Swift 3中,他们没有充分理由改变了这个功能。你应该

var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))

请注意,参数不仅反转,而且repeatedValue: -> repeating: