-2

I'm migrating my Swift code from version 2 to 3 and there is this issue:

Argument labels '(count:, repeatedValue:)' do not match any available overloads

My code

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
}
Alexandre Lara
  • 2,464
  • 1
  • 28
  • 35
  • 4
    The compiler will suggest the Swift 3 syntax (two-step). Just follow it. – vadian Mar 03 '17 at 11:23
  • @vadian clearly you didn't test the code in your compiler, otherwise you would see that there is no Swift 3 syntax suggestion for this – Alexandre Lara Mar 03 '17 at 11:38
  • 4
    I **did** test the code. First error is: *Argument `repeatedValue` must precede `count`* with a fix suggestion. Second error is: *Incorrect argument label in call (expected 'repeating:count:'*) also with a fix suggestion. – vadian Mar 03 '17 at 11:42
  • By the way: Your code works only if the expected address is always the last item in the loop. – vadian Mar 03 '17 at 11:51
  • 2
    I don't know where your code is from, but is looks quite similar to the function posted here: http://stackoverflow.com/a/30754194/1187415, which has been updated for Swift 3 some time ago. – Martin R Mar 03 '17 at 12:04

1 Answers1

11

In Swift 3 they changed the function for no good reason. You should have

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

Note that, not only are the arguments reversed but repeatedValue: -> repeating:

JeremyP
  • 84,577
  • 15
  • 123
  • 161