HELP!! how to add objects from directory into nsmutablearray?

Discussion in 'iOS Programming' started by mandude, Jun 14, 2010.

  1. mandude macrumors member

    Joined:
    Nov 19, 2009
    #1
    ok so in the left hand tab of my xcode window i have a folder names "Cards" that contains 52 PNG images of playing cards. I want to use initWithContentsOfURL: to insert all the PNG Images into this NSMutableArray. But how can i tell it to insert the contents of the folder? im stumped please help!



    Code:
    
    array = [NSMutableArray alloc] initWithContentsOfURL: ?????:  ];
    
    
    what do i write there in order to insert the contents of the folder in the side bar?? should i be doing

    Code:
    
    NSURL *cardsPath = *// path to the folder with card PNG's;
    
    array = [NSMutableArray alloc] initWithContentsOfURL:cardsPath];
    
    [cardsPath release];
    
    
    if that would work i still need to know how to write out the path to the folder thanks!
     
  2. skunkworker macrumors regular

    Joined:
    Sep 9, 2007
    #2
    InitWithContentsOfURL wouldn't work for loading in a directory how you have asked.
    It may be easier to use a for loop to load them in.

    I am assuming that this is on a iPhone OS/iOS device.

    Code:
    
    NSMutableArray *myArray = [NSMutableArray array];
    for (int i=0; i< 52; i++)
    {
    	NSString *filePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d",i] ofType:@"png"];  
    	UIImage* image =  (UIImage*)[UIImage imageWithContentsOfFile:filePath];
    	[array addObject:image];
    }
    
     
  3. chown33 macrumors 604

    Joined:
    Aug 9, 2009
  4. mandude thread starter macrumors member

    Joined:
    Nov 19, 2009
    #4


    ok thats better now how do i include the path of the directory in this loop?
     
  5. mandude thread starter macrumors member

    Joined:
    Nov 19, 2009
    #5
    can anyone help me put in the path of my directory into this loop? im completely stuck on this and ive read 100 different ways to do it but im not experienced to know please someone show me
     
  6. PatrickCocoa macrumors 6502a

    Joined:
    Dec 2, 2008
    #6
    Why bother with a loop? Just write 52 statements:

    [cardArray addObject:card1.png];
    .
    .
    .
    [cardArray addObject:card52.png];
     
  7. mandude thread starter macrumors member

    Joined:
    Nov 19, 2009
    #7


    Would that even work? Because in order to do that I think I had to do this

    Code:
    Uiimage *card1 = [uiimage setimage:@"aceofhearts.png"]
    
    array = [[nsmutablearray alloc] initwithobjects: card1, card2,... Card 52, nil];
    
    Writing out all 52 doesn't sound so fun that way
     
  8. PatrickCocoa macrumors 6502a

    Joined:
    Dec 2, 2008
    #8
    You're right, my example was incomplete, you'll need a couple of statements for each card.

    Regarding "not so fun", ask yourself "what is the objective of this piece of code?". It's to get images into an array. What's the hang-up with using loops rather than 52 (or 104) separate statements? You've already spent a couple of days on the loop method, maybe it's time to move on.

    Long term, it would be valuable to know how to do this using a loop. So make an explicit decision - do I want to spend the time to become smarter, or do I want to complete my application? There will be another 50 or 100 similar issues as you work on your application, so be prepared.
     
  9. PatrickCocoa macrumors 6502a

    Joined:
    Dec 2, 2008
    #9
    Not that anyone asked, but the cool kids don't use indices in their loops anymore. They would set up an array with 52 entries (maybe the names), then:
    Code:
    for (*element* in *array*)
    {
       some sort of [array addObject:card image];
    }
     
  10. skunkworker macrumors regular

    Joined:
    Sep 9, 2007
    #10
    Well what you are referring to is the built in fast enumerator. Which yes is handy to use when you are retrieving objects from an array and faster at that, but in my example I am adding things into array. Since I am adding, the fast enumerator in this case has nothing to transverse so a regular for loop is used as it is the best thing to do.

    Code:
    // for putting into array
    for (int i=0; i<100; i++)
    {
         [myArray addObject:[MyClass createObjectWithString:[NSString stringWithFormat:@"String %d",i]];
    }
    // for pulling out of array
    for (MyClass *object in myArray)
    {
      NSLog(@"Printing myClass string:%@",[object getString]);
    }
    
    
     

Share This Page