How to correct the wording of the C-style?

What is the correct way to fix C-style for a statement for the code below?

I am currently getting this war:

C-style for approval is outdated and will be removed in a future version of Swift

var ifaddr : UnsafeMutablePointer<ifaddrs> = nil
if getifaddrs(&ifaddr) == 0 {
    // Warning
    for (var ptr = ifaddr; ptr != nil; ptr = ptr.memory.ifa_next) {
        // Do some stuff...
    }
}
+4
source share
3 answers

You can convert a loop forto a loop while:

var ptr = ifaddr
while ptr != nil {
    // Do stuff
    ptr = ptr.memory.ifa_next
}
+7
source

Here is the version that worked for me.

var ptr = ifaddr
repeat {
   ptr = ptr.memory.ifa_next
   if ptr != nil {
      ...
   }
} while ptr != nil
+3
source

Swift 3 sequence , " ". :

var ifaddr : UnsafeMutablePointer<ifaddrs>?
if getifaddrs(&ifaddr) == 0 {
    if let firstAddr = ifaddr {
        for ptr in sequence(first: firstAddr, next: { $0.pointee.ifa_next }) {
            // ...
        }
    }
    freeifaddrs(ifaddr)
}
0

All Articles