2016-06-12 41 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 
} 
相關問題