-3

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

https://stackoverflow.com/questions/37814867/fix-c-style-for-statement-is-deprecated-in-swift

Community
  • 1
  • 1
Jordan
  • 424
  • 5
  • 19
  • 2
    Update your question with your attempt to write the `for` loop in Swift 3 and explain what issues you are having with it. – rmaddy Oct 17 '16 at 18:23
  • 1
    Did you look at any sample code whatsoever? – gnasher729 Oct 17 '16 at 18:24
  • There's no need to use `self.` everywhere; that's just decreasing readability. – Graham Perks Oct 17 '16 at 18:27
  • 1
    `for i in 0 ..< self.senderArray.count` – as for example demonstrated in your first reference – did not work? – Martin R Oct 17 '16 at 18:28
  • @MartinR No that did not work – Jordan Oct 17 '16 at 18:29
  • @Jordan What is the issue you are facing when you tried MartinR's suggestion ? – Midhun MP Oct 17 '16 at 18:30
  • @gnasher729. Yes, I do look at sample code, but I just can't figure out why `for i in 0 ..< self.senderArray.count` doesn't work. Nevermind the answer below has fixed my problem. Now I know where I went wrong at least I can read about it! – Jordan Oct 17 '16 at 18:32
  • @MidhunMP thanks, but the answer below has fixed it – Jordan Oct 17 '16 at 18:33
  • 1
    *"Now I know where I went wrong"* – I am glad that your problem is solved. Please update your question so that the actual problem becomes apparent not only to you but also for us. Questions without a clear problem statement have no value for future readers of this thread and are off-topic. – Martin R Oct 17 '16 at 18:35
  • 1
    @MartinR Thank you for understanding. It all makes sense when someone else points out the problem! – Jordan Oct 17 '16 at 18:41
  • Sorry, but I did *not* understand your problem, and you did not demonstrate *why* `for i in 0 ..< self.senderArray.count` does not work, and what the actual problem was. – Martin R Oct 17 '16 at 18:46
  • Thanks for pointing that out. I'll update that now – Jordan Oct 17 '16 at 18:47

1 Answers1

4

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])

}
Graham Perks
  • 23,007
  • 8
  • 61
  • 83