Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

AbhishekApple

macrumors member
Original poster
Aug 5, 2010
86
0
Like in java can we create objects array in objective c
Code:
 Film Watch[] = new Film[4];
        Watch[0] = new Film("Shrek",133);
        Watch[1] = new Film("Road to Perdition",117);
        Watch[2] = new Film("The Truth about Cats and Dogs",93);
        Watch[3] = new Film("Enigma",114);
 
Research NSArray.

(This should have been posted to the iPhone programming forum at https://forums.macrumors.com/forums/135/.)

i want to create objects dynamically
like

Code:
- (void)viewDidLoad {
    [super viewDidLoad];
	for (int i=0;i<5;i++) {
		NSString *str = [NSString stringWithFormat:@"chk%@",i];
		Checkbox str =[[Checkbox alloc]init];
	}
}

But the objects created inside a method cant be accessed outside. so what i need is

Code:
Checkbox chkno[];

- (void)viewDidLoad {
    [super viewDidLoad];
	for (int i=0;i<5;i++) {
		chkno[i] =[[Checkbox alloc]init];
	}
}

-(void)check{

chkno[0].Status =NO;

}
 
You want NSMutableArray. Many classes have a mutable (i.e. modifiable) subclass (e.g. NSArray -> NSMutableArray, NSString -> NSMutableString, NSDictionary -> NSMutableDictionary, etc.).

crackpip
 
Found the solution thanks to RLScott

Film *Watch[4];

Watch[0] = [[Film alloc] initWithTitle: @"Shrek" andNumber: 133];
Watch[1] = [[Film alloc] initWithTitle: @"Road to Perdition" andNumber: 117];
Watch[2] = [[Film alloc] initWithTitle: @"The Truth about Cats and Dogs" andNumber: 93];
Watch[3] = [[Film alloc] initWithTitle: @"Enigma" andNumber: 114];
 
Found the solution thanks to RLScott

Film *Watch[4];

Watch[0] = [[Film alloc] initWithTitle: @"Shrek" andNumber: 133];
Watch[1] = [[Film alloc] initWithTitle: @"Road to Perdition" andNumber: 117];
Watch[2] = [[Film alloc] initWithTitle: @"The Truth about Cats and Dogs" andNumber: 93];
Watch[3] = [[Film alloc] initWithTitle: @"Enigma" andNumber: 114];

This will most likely leak memory. If you are coming from Java i suggest going through a good objective-c book to ensure you produce a reliable application.
 
This will most likely leak memory. If you are coming from Java i suggest going through a good objective-c book to ensure you produce a reliable application.

if do this i think memory leak will not happen n it is woking fine in my project...

Code:
for(i=0;i<5i++)
[ObjectName[i] release];
 
It seems like when you would want to do something like this, you would almost always want a variable sized array, in which case NSMutableArray would be a better choice. It also seems like it would be really easy to mess up the memory management. Is there a specific reason you want to use C Arrays instead? In my experience it is almost always better to go with the standard classes instead of rolling your own.
 
if do this i think memory leak will not happen n it is woking fine in my project...

Code:
for(i=0;i<5i++)
[ObjectName[i] release];

Look, that is just ridiculous.

It seems like every question you ask on here indicates a lack of basic knowledge. People are here to help, but they aren't going to hold your hand and teach you everything from scratch if you cant at least make the effort to teach yourself the basics.

Please, for your own sake, get a good book on Objective C (the Kochan book is highly recommended) and read it twice before going any further otherwise you are just going to make a complete mess of things.
 
Code:
for(i=0;i<5i++)
[ObjectName[i] release];

Look, that is just ridiculous.

Also, it's not all about releasing. The full object ownership convention says all objects added to a collection must receive a retain message. Otherwise, as it was said, memory can be messed up quite easily.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.