问题
I have an NSMutablearray of objects. the number of objects is set by user. in c++ I would use a for cycle and the 'new' command.something like this:
int fromuser, a;
for(a=0;a<fromuser;a++){
array addobject:(new class obj)
}
what do I need to do in obj c since there is no new?
回答1:
You would utilize the alloc
and init
(or more specialized initializer) provided by NSObject.
For example, something like the following should work:
int fromuser, a;
NSMutableArray objectArray = [[NSMutableArray alloc] initWithCapacity:fromuser];
for (a = 0; a < fromuser; a++)
{
MyObject *obj = [[MyObject alloc] init];
[objectArray addObject:obj];
[obj release]; //If not using ARC
}
来源:https://stackoverflow.com/questions/8748114/new-and-delete-command-of-c-in-obj-c