1

I realize this question is pretty basic, but I'm really stuck. I have a plist. I'm trying to read that into an array so I can work with it in various classes. So in one class I have:

+ (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; } 

I want to be able to access the things I pull out of the array from another class. I tried calling it like NSString *str = [QuestionContainerObject questionArray]; from my other class (after importing the header) but I get the 'class method +questionArray not found' warning.

Can someone please point me in the right direction? I'm really lost! Thanks!!

3
  • 1
    The warning suggest you have not declared the questionArray class method in the header file, or it is not declared in the @interface block for the class QuestionContainerObject. What happens when you run it does it work, or does it throw an exception. Commented Mar 23, 2012 at 0:58
  • whoops, you are so right. Fixed that, now everything is working. Is this the proper way to call the method though? Do I always need to reference my QuestionContainerObject? Thanks, I've never worked with class methods before. Commented Mar 23, 2012 at 1:03
  • Yes that us correct, you can also from within a class method do [self otherClassMethod], classes are objects in Objective-C. If you are familiar with the factory object in design patterns, the class object is the factory object for instances of that class. Commented Mar 23, 2012 at 1:37

2 Answers 2

2

The warning is because the compiler does not know the questionArray method exists. define this method in the header (QuestionContainerObject.h)

@interface QuestionContainerObject + (NSArray*)questionArray; @end 

and in the file using it:

#import QuestionContainerObject.h 

Also remember to release objects created using [class alloc]

Sign up to request clarification or add additional context in comments.

3 Comments

not if he's using arc .. it should be mandatory for iOS questions to include wether they use arc or not :D
SHE's not using ARC ;) Thanks for your help. Is this the proper way to call the method though? Do I always need to reference my QuestionContainerObject? Thanks, I've never worked with class methods before.
Since classes are actually just objects as well, all class methods are messages sent to the class. So for instance: [ClassName someClassMethod]; and you can call that from any class, and without instantiating an instance of the class first.
0

Did you put the class method declaration in the header file?

Btw, move the static NSArray* questions = nil; outside of the method body and remove the = nil, or else its not going to have the effect you wanted, i think.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.