0

I'm trying to use a loop like this:

    for var i : CGFloat = 0; i<3 ; i+=1  {
    // code
    }

xcode tells me that this C-style statement is deprecated and will be removed in the future. This is just a warning, not an error. So i try to fix this using the following loop:

      for i : CGFloat in 0 ..< 3     {
    //code
    }

but i get this error : "Binary operator '..<' cannot be applied to two 'Int' operands"

Does anyone know how to fix this ?

Julien

JulienCoo
  • 1,128
  • 3
  • 13
  • 24
  • 0 and 3 are Ints, but `i` is CGFloat. Convert one to the other, you can't do math on apples and oranges. ;) – Eric Aya May 02 '16 at 08:59
  • I don't see why you need a `CGFloat` here, you're just adding 1 at each iteration. – Hamish May 02 '16 at 09:00
  • Take note of originaluser2:s note above (i.e., do you really want a `CGFloat` as the invariant in your loop?), but for the technical aspect: one possible equivalent loop is to use `stride(to:by)` as follows: `for i in CGFloat(0).stride(to: 3, by: 1) { ... }` (included as the thread marked as duplicate don't really mentioning looping). – dfrib May 02 '16 at 09:24
  • @EricD The Q&A ['Replacement for C-style loop in Swift 2.2 CGFloat'](http://stackoverflow.com/questions/36289786/replacement-for-c-style-loop-in-swift-2-2-cgfloat) is, imho, probably a more appropriate duplicate, as the `CGFloat`/`Int` type mismatch in this question is just a "side effect" of what the OP:s own attempt of trying to construct a Swift 2.2-valid loop with a `CGFloat` iterator. – dfrib May 02 '16 at 09:30
  • @dfri Right, it's a better one indeed. Unfortunately I can't re-close the question to a different target if I re-open it, so I can't change the link in the yellow box. – Eric Aya May 02 '16 at 09:35
  • @EricD Ah I see, I didn't know that limitation. I guess the current duplicate is somewhat relevant to the topic of this question (which might be what brings people in here), even if not spot-on to the content, so lets just leave it as it is (a duplicate! :) – dfrib May 02 '16 at 09:40

0 Answers0