views:

39

answers:

1

Hi, this is a bit of a newbie question but I can't seem to find it anywhere. I have defined a class in a set of .h/.m files and have a separate .h/.m for drawing. What I'm trying to do is create an array of objects from this class, and draw them sequentially to the screen.

Of course, I'm getting a 'squares' undeclared (first use in this function) error. I went around in circles for a while, with lots of duplicate code, before I admitted that I don't know how to do this and went to seek help here.

In one .h file: @interface DrawerViewController : UIViewController {
NSMutableArray *squares;
}
@property (nonatomic, retain) NSMutableArray *squares;
@end

In the corresponding .m: squares = [[NSMutableArray alloc] init];

In another .m file, where I want to access the "squares" array:

if ([squares objectAtIndex:thisID] != NULL) {

And that's where I'm getting the error: "squares" undeclared. I'm using include to bring in the other .h file, but that doesn't seem to be working.

If someone can point me in the right direction, I will be very grateful...

A: 

The 'squares' attribute is a member of another object, therefore you can't directly send messages to it. In the second .m file, create an object of type DrawerViewController and call 'if ([[myDrawerViewController squares] objectAtIbdex: thisId])'. If you've #included the other file that should compile fine. In essence, you're nesting two message calls there... The first retrieves the squares property and the second sends a message to that proprty asking for the array member.

samkass
Hmm, I'm getting `'DrawerViewController' may not respond to '+squares'`. Have I missed a step? Sorry...
ickydog
Yes, you do not ask for `[DrawerViewController squares]` because that would give you the error you're seeing, you need to instantiate an object of type `DrawerViewController`, called (for example) `myDrawerViewController`, and then do as @samkass said. In this sense Objective C is no different from Java or C++ - you need to invoke a method on an object.
Echelon
(You'll also need a `@synthesize squares;` in your implementation of `DrawerViewController`. That _is_ unique to ObjC.)
Echelon
Ah! Thanks samkass and Echelon, I get it now!
ickydog