-4

++ and -- has been deprecated in swift as of the latest release and will be removed in Swift 3.

I have this code that is currently working that I want to update, but I am not sure how to rewrite it. Any help would be appreciated.

for var i = 2; i <= remarkIndex; ++i {
    if containsWind(metarArray[i]) {
        metarDict.updateValue(metarArray[i], forKey: "winds")
    }
}

I realize there's a different way Swift likes to make loops, but I am inexperienced with it. When I wrote all of this code, I just used the C++ way I was used to. Looking to have an example I understand that I can apply across my code.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
Charlie
  • 1,279
  • 2
  • 12
  • 28

2 Answers2

3

Just for completeness, let's show a more functional approach for the inner block, too:

metarArray[2...remarkIndex]
   .filter(containsWind)
   .forEach { metarDict.updateValue($0, forKey: "winds") }
Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • Oh my this is elegant and looks much less buggy. Will need to sit down and learn these syntax options. Thanks so much for your post. – Charlie Mar 25 '16 at 17:16
  • This is certainly the Swiftiest way, but note that it entails an extra expense, as we must cycle through the slice _twice_. In your actual code, that expense is likely to be negligible, however. – matt Mar 25 '16 at 18:06
  • @matt I completely agree that functional approach is not always the best approach. – Sulthan Mar 25 '16 at 18:11
  • But it is prettiest! – matt Mar 25 '16 at 18:13
2

Like this:

for i in 2...remarkIndex

This is what you should always have been doing anyway.

However, since the only thing you want to do with i is to index into metarArray, it would be even better to cycle thru metarArray itself:

for item in metarArray[2...remarkIndex]

No index required!

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thanks for the short answer that gets right to the point. I am learning! Huzzah. – Charlie Mar 25 '16 at 16:47
  • But even better, since you only want to work with `metarArray[i]`, would have been to cycle through `metarArray` _itself_. Indexing successively into an array is completely unnecessary. – matt Mar 25 '16 at 16:49
  • Removing the outer loop does make a lot of sense now that you mention it. I appreciate that comment. – Charlie Mar 25 '16 at 16:50
  • Also the fact that you want to start at index 2 has a bad smell. But that's another issue. – matt Mar 25 '16 at 16:51
  • Agree, the only reason that is there is the formatted data is always beginning at position 2 from the govt wind reports. Thanks for all of these replies, I am still learning so it is a huge help. – Charlie Mar 25 '16 at 16:53
  • See my revised answer. You can slice out the desired section of the array and just cycle thru the _items_ of the slice. No indexing required! – matt Mar 25 '16 at 16:54
  • Remark: In OP's code, `i == remarkIndex` is *included* in the loop, so it should be `2 ... remarkIndex`. – Martin R Mar 25 '16 at 16:56
  • Thanks @MartinR I slept thru it... :) – matt Mar 25 '16 at 17:00
  • 1
    You can even use `where containsWind(item)` – Marcelo Mar 25 '16 at 17:09