views:

294

answers:

3

Ok, so I have the code below (Objective-C FYI) and I was wondering if I want to create an NSMutableArray of c_data objects, how would I go about doing that? It's sort of like declaring a List<c_data> cData in C#.

@interface c_data : NSObject {
    double value;
    int label;
    int ID;
}    
@property double value;
@property int label;
@property int ID;

-(c_data*) init;
-(c_data*) initWithValue:(double)value;    
@end

@implementation c_data
@synthesize value, label, ID;
-(c_data*) init {
    return self;
}
-(c_data*) initWithValue:(double)val {
    value = val;
    return self;
}
@end

If you look at the class feat_data, I'm trying to make cData an array of the class c_data. I have included my attempts at it, but I don't think it's right because c_data isn't an array. Any suggestions?

@interface feat_data : NSObject {
    NSMutableArray *nData;
    NSMutableArray *cData;
    char type;
}
@property(nonatomic, retain) NSMutableArray *nData;
@property(nonatomic, retain) NSMutableArray *cData;
@property char type;
-(feat_data*)init;
@end

@implementation feat_data
@synthesize nData, cData, type;
-(feat_data*)init {
    nData = [[NSMutableArray alloc] init];
    c_data *c_dataInstance = [[c_data alloc] init];
    cData = [[NSMutableArray alloc] initWithArray:c_dataInstance];
    return self;
}
@end
+2  A: 

You would create an NSMutableArray and insert c_data objects into it.

Chuck
+3  A: 
[NSMutableArray addObject:[[[c_data alloc] init] autorelease]];

Objective-C arrays aren't typed. It seems you have some C++ unlearning to do.

On a related note, your inits are pretty bad. You need to call super init as well, as such:

- (id)init {
  self = [super init];
  if (self != nil) {
    //Initialize here.
  }
  return self;
}
Williham Totland
ah, thanks for the help. On a related but completely different topic, what c++ book would you suggest buying that encompasses most of the programming concepts? The one I have got me through the syntax pretty good, but that's about it. It doesn't discuss much past that. I don't mind paying a huge price if it's a great book.
Josh Bradley
You're asking the wrong guy. I'd recommend not buying a C++ book at all. But if you meant what *Objective-C* book I'd suggest; I'd say taking a long hard look at Apple's programming references at http://developer.apple.com/ is a good start.
Williham Totland
That's a memory leak if you aren't using garbage collection. You need to release the object after you add it to the array.
Chuck
Indeed it is; fixed.
Williham Totland
+3  A: 

There is no such thing as statically typed / template / generic collections in Objective-C. Basically, the point of a strongly-typed collection is to provide static type safety at compile time. Such an approach makes little sense in a language as dynamic as Objective-C. The approach to the problem of disparate object types in Objective-C collections is to only insert the appropriate object type(s). (Also, remember that the array will retain objects it contains, so if you insert a new object without releasing and you lose the pointer to it, you're leaking memory.)

If you think about it, one of the biggest benefits to generics is being able to retrieve objects from the collection directly into a statically-typed variable without casting. In Objective-C, you can just store to an id variable and send whatever message you like without fretting about a ClassCastException, or the compiler complaining that an object doesn't (may not?) implement the method you're attempting to invoke. You can still statically type variables and cast results if desired, but the easier approach is to use dynamic typing (and -isKindOfClass: and -respondsToSelector: if necessary).

Incidentally, there are several related incarnations of this question on Stack Overflow. Knowing the term(s) to search for ("generic", "strongly-typed", or "template") can help find them. Here are a few:

Finally, I agree with William — your init methods are pretty egregious in the sample you provided. You'd do well to learn and heed Apple's rules of Allocating and Initializing Objects in Objective-C. It requires breaking habits from other languages, but it will save you endless hours of insanity at some point down the road. :-)

Quinn Taylor
Thanks. I noticed you've answered several of my questions in the past. You've been a great help to me this whole summer.
Josh Bradley