1

So Swift 3 adds sequences, the following code works fine in Swift 3 however I am trying to convert it back to Swift 2.3

What would I replace sequence with in swift 2.3?

func isWifiEnabled() -> Bool {
        var addresses = [String]()

        var ifaddr : UnsafeMutablePointer<ifaddrs>?
        guard getifaddrs(&ifaddr) == 0 else { return false }
        guard let firstAddr = ifaddr else { return false }

        for ptr in sequence(first: firstAddr, next: { $0.pointee.ifa_next }) {
            addresses.append(String(cString: ptr.pointee.ifa_name))
        }

        freeifaddrs(ifaddr)
        return addresses.contains("awdl0")
    }
Burf2000
  • 5,001
  • 14
  • 58
  • 117
  • 1
    http://stackoverflow.com/a/25627545/1187415 has code to enumerate all interfaces for both Swift 2 and 3. – Martin R Jan 11 '17 at 16:25

1 Answers1

1

This worked for Swift 2.3 but will of course not work for Swift 3

 func isWifiEnabled() -> Bool {
    var addresses = [String]()

    var ifaddr : UnsafeMutablePointer<ifaddrs> = nil
    guard getifaddrs(&ifaddr) == 0 else { return false }

    var ptr = ifaddr
    while ptr != nil { defer { ptr = ptr.memory.ifa_next }
         addresses.append(String.fromCString(ptr.memory.ifa_name)!)
    }

    var counts:[String:Int] = [:]

    for item in addresses {
        counts[item] = (counts[item] ?? 0) + 1
    }

    freeifaddrs(ifaddr)
    return counts["awdl0"] > 1 ? true : false
}

Updated I also noticed that it was listing awdl0 twice? Maybe a new thing to iOS 10.2

Burf2000
  • 5,001
  • 14
  • 58
  • 117