0

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) NSString *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

2 Answers 2

2

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. :)

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

1 Comment

I would prefer a recursive autoreleased method myself
2

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;

2 Comments

do not forget to release your view objects - your code leaks memory
Also note that if you want to create something equivalent to an ArrayList, you want to use the mutable (aka modifiable) version of NSArray, it's subclass NSMutableArray

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.