newbie confused by obj-c syntax
newbie confused by obj-c syntax
- Subject: newbie confused by obj-c syntax
- From: Daniel Child <email@hidden>
- Date: Mon, 14 Mar 2005 22:54:55 -1000
Hi All,
I am trying to use my own homegrown array of a class I created, called StrokeDescription. This class has two member variables: desc (description) and strokeType.
Now I want to have a class that stores an array of these StrokeType objects. Somehow this didn't seem so hard in Java. I am getting hung up in Objective-C syntax and was hoping you could help out.
Three questions:
1) Is it better to always use NSArray and NSMutableArray classes, rather than create your own classes of homegrown class objects.
2) How do you return an array (and not lose it).
3) Compared to Java, the declarations look
wrong to me, even though they generated less errors than when I attempted something Java-like. Are these really OK?
Here's what I have:
#import <Foundation/Foundation.h>
#import "StrokeDescription.h"
@interface StrokesList : NSObject {
int numInList;
StrokeDescription *knownStrokes[100];
}
- (void)addStrokeDesc:(StrokeDescription *)sd;
- (StrokeDescription *[])getStrokesOfType:(int)st;
- (int[])getStrokeTypesForDescription:(NSString *)d;
@end
#import "StrokesList.h"
#import "StrokeDescription.h"
@implementation StrokesList
- (void)addStrokeDesc:(StrokeDescription*)sd; {
knownStrokes[numInList] = sd;
numInList++;
}
- (StrokeDescription *[])getStrokesOfType:(int)st {
StrokeDescription *matchingType[100];
int i, found = 0;
for (i = 0; i < numInList; i++) {
if ([knownStrokes[i] type] == st) { // if it matches the stroke type parameter
matchingType[found] = knownStrokes[i]; // add to the array of matching stroke types
}
} // end for
return matchingType; // return the array THIS LINE DOES NOT WORK
// also error about returning a local variable (I will lose this?)
}
- (int[])getStrokeTypesWithDescription:(NSString *)d {
// CONFLICTING TYPE WARNING EVEN THOUGH THE h FILE HAS THE SAME LINE OF CODE
// ANOTHER WARNING ABOUT DECLARED AS FUNCTION RETURNING AN ARRAY
int i = 0, found = 0;
int types[10];
NSString *thisDesc;
for (i = 0; i < numInList; i++) {
thisDesc = [knownStrokes[i] desc];
if ([thisDesc isEqualToString: d]) {
types[found] = [knownStrokes[i] type];
found++;
}
}
return *types;
}
@end
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden