can we create objects Array in objective c?

Discussion in 'iOS Programming' started by AbhishekApple, Sep 8, 2010.

  1. AbhishekApple macrumors member

    Joined:
    Aug 5, 2010
    #1
    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);
    
     
  2. ianray macrumors 6502

    Joined:
    Jun 22, 2010
    Location:
    @
    #2
  3. AbhishekApple thread starter macrumors member

    Joined:
    Aug 5, 2010
    #3
    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;
    
    }
    
     
  4. AbhishekApple thread starter macrumors member

    Joined:
    Aug 5, 2010
  5. crackpip macrumors regular

    Joined:
    Jul 23, 2002
    #5
    You want NSMutableArray. Many classes have a mutable (i.e. modifiable) subclass (e.g. NSArray -> NSMutableArray, NSString -> NSMutableString, NSDictionary -> NSMutableDictionary, etc.).

    crackpip
     
  6. AbhishekApple thread starter macrumors member

    Joined:
    Aug 5, 2010
    #6
    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];
     
  7. danielbroad macrumors newbie

    danielbroad

    Joined:
    Sep 8, 2010
    Location:
    United Kingdom
    #7
    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.
     
  8. AbhishekApple thread starter macrumors member

    Joined:
    Aug 5, 2010
    #8
    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];
    
     
  9. seepel macrumors 6502

    seepel

    Joined:
    Dec 22, 2009
    #9
    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.
     
  10. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #10
    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.
     
  11. CMT macrumors member

    CMT

    Joined:
    Aug 24, 2009
    #11
    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.
     

Share This Page