Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix C-style for statement?

Tags:

ios

swift

swift2

What is right way to fix C-style for statement for the code which is posted below?

Currently I am getting this warring:

C-style for statement is deprecated 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...
    }
}
like image 486
Ramis Avatar asked Dec 25 '22 07:12

Ramis


2 Answers

You could convert the for loop into a while loop:

var ptr = ifaddr
while ptr != nil {
    // Do stuff
    ptr = ptr.memory.ifa_next
}
like image 199
JAL Avatar answered Dec 26 '22 21:12

JAL


Here's a version that worked for me.

var ptr = ifaddr
repeat {
   ptr = ptr.memory.ifa_next
   if ptr != nil {
      ...
   }
} while ptr != nil
like image 38
Telstar Avatar answered Dec 26 '22 22:12

Telstar