2016-06-12 38 views
-5

我已经宣布在Objective-C项目自定义对象数调用自定义对象:其名称中包含在Objective-C

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

我怎么能叫他们在周期

for (int i=1; i<=6; i++) 
{ 
} ? 
+2

您应该使用数组这个 – Hamish

+0

**你不要**使用数组! – luk2302

回答

3

你可以没有直接做到这一点。您可以使用数组(C风格或NSArray),然后迭代(使用从零开始的索引或for-in循环)。

例如:

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 
} 
相关问题