0

I have three NSRects in separate variables named rect1, rect2, and rect3, and I want to increment each one's origin.x by 10.

I thought a for-loop would be good for this, but I don't know how I can do it.

This is an example of how I want it to work:

for(int i=0, i<3, i++) {
   rect[i].origin.x +=10
}

but this exact code gives an error

property "rect" not found on object of type "graphics"

Is there a way to code it like in my example?

jscs
  • 63,694
  • 13
  • 151
  • 195
user3195648
  • 67
  • 1
  • 1
  • 6
  • What problem are you having? – rmaddy Oct 19 '14 at 17:49
  • It wont accept "rect[i]" – user3195648 Oct 19 '14 at 17:51
  • 1
    Be more specific. Define "won't accept". If you are getting a warning or error, post the exact message. Also tell us how `rect` is declared. – rmaddy Oct 19 '14 at 17:53
  • Ok sorry, they are all decleared as NSRect so for example: NSRect rect1; NSRect rect2; The error that it produces is: "property "rect" not found on object of type "graphics" – user3195648 Oct 19 '14 at 17:55
  • If you have only 3 NSRect why the for loop ? – GoodSp33d Oct 19 '14 at 17:57
  • @user3195648 You can't access individual variables using array syntax. – rmaddy Oct 19 '14 at 17:57
  • rect[i] means the i th number of rect in the array,but apparently you are trying to use it to access rect'i' (the name) variable instead – Amir Oct 19 '14 at 17:57
  • yes that's why I ask if there is a way to do it for the variable? – user3195648 Oct 19 '14 at 18:01
  • See also [ObjC equivalent of PHP's "Variable Variables"](http://stackoverflow.com/questions/2283374/objective-c-equivalent-of-phps-variable-variables), [Create multiple variables based on an int count](http://stackoverflow.com/q/2231783), and [Is it possible to reference a variable with a string and an int?](http://stackoverflow.com/q/6049175) – jscs Oct 19 '14 at 18:11

1 Answers1

1

Is there a way to increment a variable with for loop

Not in objective-c for sure. You cannot declare variables in 0-n format and loop through it.

If you create an array out of your NSRect variables then you can loop through the array and modify properties.

Note: NSArray can only hold Objects not primitives, you might find this handy

GoodSp33d
  • 6,252
  • 4
  • 35
  • 67