0

Having some difficulty with what I thought would be straight forward. I am trying to make an array of class objects but running into different problems. The goal was to create one class that held an array of another class of objects.

Any help appreciated:

// Basic class unit
@interface MobRec : NSObject {
    NSString *MName;
    int Speed;
}

@end


// Master Class holding an array of units
@interface MobDefs : NSObject {
    MobRec *MobInfo;
}

@property(retain) MobRec *MobInfo; 

@end


@synthesize MobInfo;

1) From reading it seems I should create and NSMutableArray but how do you declare an NSMutableArray of custom class objects? All iterations I try cause errors. Previously I had predefined the size in the class as MobInfo[20]; but that didnt seem to be good for anything.

2) How do you properly @Synthesize an array of class objects?

1
  • 2
    I highly recommend following the Objective-C naming conventions. Classes are capitalized; methods and properties get a lowercase first letter. So it would be MobRec *mobInfo. Commented Dec 30, 2009 at 21:14

3 Answers 3

3

I think you misunderstand what @synthesize does. It creates accessor methods to get and set the property (i.e., it would create a getter method to return that NSMutableArray and a setter method to allow you to replace it with another NSMutableArray). To create an NSMutableArray, you would just create one like any other object in that class's initializer.

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

Comments

2

NSMutableArray doesn't have any type checking as you add (or read) from it, so you can add any objects you want to it.

In this case I'd have something like:

// MobRec Class
@interface MobRec : NSObject {
    NSString *mName;
    int speed;
}

@property(retain)NSString *name;
@property(assign)int speed;

@end

@implementation MobRec

@synthesize mName, speed;

@end


// MobDefs Class
#import "MobRec.h"
@interface MobDefs : NSObject {
    NSMutableArray *mobInfo;
}

@property(retain) NSMutableArray *mobInfo; 

@end


@implementation MobDefs

@synthesize mobInfo;

- (id)init {
    mobInfo = [[NSMutableArray alloc] init];
    MobRec *aNewMobRec = [[MobRec alloc] init];
    [mobInfo addObject:aNewMobRec]; 
    [aNewMobRec release];
}

I've included the basics for adding to the array as well so you can see how its used. Oh and don't forget you have to release the MobInfo in the dealloc method.

But mostly look at NSMutableArray

5 Comments

THIS IS WHAT I WAS LOOKING FOR! Thank you. Couldnt seem to find anywhere else the simple syntax that said NSMutableArray did not need a type check. This should help resolve my issue.
Hmmm now I am having trouble getting the MobRec class to acknowledge existence of the mobInfo array. I included mobrec.h to the mobdefs.h, but no luck. It should see it shouldnt it - this shouldnt be so difficult.
Why does the MobRec class need to be able to access the mobInfo array?
Your right I dont need too and shouldnt be. I moved code out to better organize. Thanks! Sometimes I need that kind of a question to realize I am overcomplicating it.
I find if I'm overcomplicating the design of something just stepping away from the computer and drawing out the flow of the program/method on a piece of paper helps me to see where I'm going wrong.
0

Your MobDefs class is not necessary, you can use an NSMutableArray directly. NSArray can hold objects of any type, and is declared in just the same way as any other object (such as your MName property).

1 Comment

Thanks. But there are other functions to be performed on all the objects that I will want in the MobDefs class. Also having the multiple files works best as I am not the only coder on the project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.