在Objective-C类中是否可以将另一个类的实例数组存储为属性?
简单的例子:我有一个叫做“教室”的课和一个叫做“学生”的课。
@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
我该怎么做呢?
使用NSArray
或NSMutableArray
代替:
@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对象。您甚至可以将它们混合在同一阵列中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句