ObjectiveC, creating array of classes
I have a class that contains values:
@interface Params : NSObject {
[...]
NSString *fc;
NSString *sp;
NSString *x;
NSString *y;
[...]
@property (nonatomic, assign) NSString *fc;
@property (nonatomic, assign) NSString *sp;
@property (nonatomic, assign) NSSt开发者_如何学Pythonring *x;
@property (nonatomic, assign) NSString *y;
[...]
@end
it's possible to create in objective-c an array of classes? like c# / java?
MyClass[] a = new MyClass[20] ???
a[0].fc = "asd" ?
or something equivalent?
thanks, alberto
While the standard way to do this would be with an NSArray
, you can do this with a C-style array as well:
int numObjects = 42;
Params ** params = malloc(sizeof(Param*) * numObjects);
for (int i = 0; i < numObjects; ++i) {
params[i] = [[[Params alloc] init] autorelease];
}
free(params);
However, using an NSArray
would be much simpler. :)
You would use a standard NSArray
to do this, in my example I have used views, but you can use pointers to any object.
UIView * object = [[UIView alloc] init];
UIView * object2 = [[UIView alloc] init];
UIView * object3 = [[UIView alloc] init];
NSArray * array = [[NSArray alloc] initWithObjects:object,object2,object3,nil];
[object release];[object2 release];[object3 release];//edit
UIView * test = [array objectAtIndex:0];
test.tag = 1337;
精彩评论