Jordan Jordan - 1 month ago 13
iOS Question

C-style for statement has been removed in Swift 3 (Continuos error)

I have had this problem since updating to

Swift 3
. I have tried updating this to
Swift 3
by using
stride(from:to:by:)
but I can't implement the old
Swift 2
code to the
Swift 3
code. Below I have included my code:

for var i = 0; i <= self.senderArray.count - 1; i += 1 {

if self.senderArray[i] == userName {

self.other2Array.append(self.otherArray[i])
}
else {

self.other2Array.append(self.senderArray[i])
}

self.message2Array.append(self.messageArray[i])
self.sender2Array.append(self.senderArray[i])

}


The error I am getting:


C-style for statement has been removed in Swift 3


I have tried this method below, but it doesn't work:

for i in 0 ..< self.senderArray.count


Before anyone marks this question as a duplicate I have had a look at the following questions but for some reason, I can't figure out how to implement my code into the updated
Swift 3
code.

#warning: C-style for statement is deprecated and will be removed in a future version of Swift

How to fix C-style for statement?

Fix warning "C-style for Statement is deprecated" in Swift 3

Fix "C-style for statement is deprecated" in Swift

Answer

Keeping the index is easy with enumerated:

for (i, sender) in senderArray.enumerated() {
    // Can simply use 'sender' here, no need to index into senderArray.
    if sender == userName {
       ...

    // Unchanged lines; 'i' is used as index here.   
    self.message2Array.append(self.messageArray[i])
    self.sender2Array.append(self.senderArray[i])

}
Comments