Objective C:类对象的数组作为属性

时间:2015-08-19 08:37:43

标签: objective-c arrays class properties

在Objective-C类中是否可以将另一个类的实例数组存储为属性?

简单的例子:我有一个名为" Classroom"还有一个名为" Students"。

的课程
@interface Student
    @property int m_id;
@end

...

@interface Classroom
    @property Student *m_students[20]; // this causes a compilation error: property cannot have an array or function type 'Student *[20]'
@end

我该怎么做呢?

1 个答案:

答案 0 :(得分:1)

改为使用NSArrayNSMutableArray

@interface Classroom
    @property NSMutableArray *m_students; // this causes a compilation error.
@end

然后在您的实施中:

Student *student = [[Student alloc] init];
[self.m_students addObject:student];

NSArray(及其子类NSMutableArray)可以包含任何Objective-C对象。你甚至可以将它们混合在同一个数组中。

相关问题