我意识到这个问题很基础,但我真的卡住了。我有一个列表。我试图将它读入一个数组,以便我可以在各种类中使用它。所以在一节课中我有:
+ (NSArray*)questionArray
{
static NSArray* questions = nil;
if(!questions)
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"Questions" ofType:@"plist"];
NSDictionary *dic = [[NSDictionary alloc] initWithContentsOfFile:path];
NSArray *questionsArray = [dic objectForKey:@"groups"];
NSMutableArray *questionObjects = [[NSMutableArray alloc] initWithCapacity: [questionsArray count]];
for(NSDictionary* questionDic in questionsArray)
{
QuestionContainerObject* object = [[self alloc] initWithDictionary:questionDic];
[questionObjects addObject:object];
[object release];
}
questions = questionObjects;
[dic release];
}
return questions;
}
我希望能够访问我从另一个类的数组中提取的内容。我尝试从我的其他类(导入 header 后)像 NSString *str = [QuestionContainerObject questionArray];
那样调用它,但我收到“类方法 +questionArray 未找到”警告。
有人可以指出我正确的方向吗?我真的迷路了!谢谢!!
最佳答案
警告是因为编译器不知道 questionArray 方法的存在。 在 header (QuestionContainerObject.h) 中定义此方法
@interface QuestionContainerObject
+ (NSArray*)questionArray;
@end
并在使用它的文件中:
#import QuestionContainerObject.h
还记得释放使用[class alloc]创建的对象
关于iphone - 从另一个类调用类方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9832745/