Trouble with NSMutableArray

Discussion in 'Mac Programming' started by isthisonetaken, Jan 27, 2011.

  1. isthisonetaken macrumors regular

    Joined:
    Jun 29, 2006
    #1
    I am having some problems with NSMutableArray. I have a folder of pictures that I am trying to load into an array so I can access them in my application. I have the following code:

    Example.h
    Code:
    #import <Cocoa/Cocoa.h>
    
    @interface Example : NSObject <NSApplicationDelegate> {
        NSWindow *window;
    	
        // data
        NSMutableArray *picturesArray;
    }
    
    @property (assign) IBOutlet NSWindow *window;
    @property (assign) NSMutableArray *picturesArray;
    
    - (IBAction)loadPicturesIntoArray:(id)sender;
    - (IBAction)viewPictures:(id)sender;
    @end
    
    Example.m
    Code:
    @implementation Example
    
    @synthesize window;
    @synthesize picturesArray;
    
    - (IBAction)loadPicturesIntoArray:(id)sender {
         // delete anything that may be in the picturesArray
         [picturesArray removeAllObjects];
    	
    	// find out how many pictures are in the pics folder
    	NSFileManager *fm = [[NSFileManager alloc] init];
    	NSArray *contents = [NSArray arrayWithArray:[fm contentsOfDirectoryAtPath:@"/Users/username/pics" error:nil]];
    	NSInteger numberOfPictures = [contents count] - 1; // because contents grabs the pics and .DS_Store
    	[fm release];
    	
    	// load the pictures into the picturesArray
    	NSString *path = @"";
    	//NSMutableArray *tempArray = [[NSMutableArray alloc] initWithCapacity:numberOfPictures];
    	for (int i = 0; i < numberOfPictures; i++) {
    		// get the path to the picture
    		path = [NSString stringWithFormat:@"/Users/username/pics/image%d.jpeg", (i+1)];
    		
    		// get the picture
    		NSImage *picture = [[NSImage alloc] initWithContentsOfFile:path];
    		
    		// load the picture into the picturesArray
    		[picturesArray addObject:picture];
    		
    		//NSRunAlertPanel(@"Image Details:", @"Path: %@\nArray Size: %ld", NULL, NULL, NULL, path, [picturesArray count]);
    		[picture release];
    	}
    }
    - (IBAction)viewPictures:(id)sender {
         NSLog(@"Array Size: %ld", [picturesArray count]);
    }
    
    My folder contains 600 pictures and I would expect that the array size would be 600, but with this code nothing even happens. If I uncomment the tempArray before the for loop and add the picture to that array, it works fine for the loadPicturesIntoArray method, but I can't access the array in the viewPictures method. I tried having the temp array and than setting the picturesArray to the temp array, but it doesn't seem to be copying the pictures over.

    I feel like I'm missing something really easy, but I can't seem to figure out what?

    Any advice?
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    Where do you set picturesArray to a pointer to an NSMutableArray?

    -Lee
     
  3. isthisonetaken thread starter macrumors regular

    Joined:
    Jun 29, 2006
    #3
    In the interface:
    // data
    NSMutableArray *picturesArray;
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    No, that declares a variable whose type is NSMutableArray *. It points to nil by default. It does not point to an object yet. Messages sent to nil are discarded. You get a return value, basically 0 cast to the appropriate type.

    You need to alloc/init an NSMutableArray and assign the pointer to picturesArray before you use it.

    -Lee
     
  5. isthisonetaken thread starter macrumors regular

    Joined:
    Jun 29, 2006
    #5
    Ah, I see. I added in the call in the applicationDidFinishLaunching and it's working now. I knew it was something small, thanks for the help Lee!
     
  6. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    Good times, glad to help.

    -Lee
     
  7. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #7
    What is your reason for not retaining the pictureArray? You forgot to allocate it, now where do you deallocate it? Maybe you should examine some sample code and find the pattern that everyone is using to do this.

    Since the pictureArray should _always_ be present, it should be allocated in init and released in dealloc.
     
  8. isthisonetaken thread starter macrumors regular

    Joined:
    Jun 29, 2006
    #8
    Do you mean deallocated like this?
    Code:
    - (void)dealloc {
    	[picturesArray release];
    	[super dealloc];
    }
     

Share This Page