-5

I have declared custom objects in Objective-c project:

Student* student1 = [[Student alloc]init];
Student* student2 = [[Student alloc]init];
Student* student3 = [[Student alloc]init];
Student* student4 = [[Student alloc]init];
Student* student5 = [[Student alloc]init];
Student* student6 = [[Student alloc]init];

How can I call them in cycle

for (int i=1; i<=6; i++)
{
}   ?
2
  • 2
    You should be using an array for this Commented Jun 12, 2016 at 9:58
  • You don't! Use an array. Commented Jun 12, 2016 at 9:58

1 Answer 1

3

You can't directly do that. You could use an array (either C-style or an NSArray) and then iterate over that (using zero-based indexes or a for-in loop).

For example:

NSArray* students = @[ [[Student alloc] init],
                       [[Student alloc] init],
                       [[Student alloc] init],
                       [[Student alloc] init],
                       [[Student alloc] init],
                       [[Student alloc] init],
                     ];
for (int i = 0; i < students.count; i++)
{
    Student* student = students[i];
    // Do something with student
}
// Or:
for (Student* student in students)
{
    // Do something with student
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.