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

isthisonetaken

macrumors regular
Original poster
Jun 29, 2006
123
0
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?
 
In the interface:
// data
NSMutableArray *picturesArray;

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
 
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

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!
 
I am having some problems with NSMutableArray. I feel like I'm missing something really easy, but I can't seem to figure out what?

Any advice?

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.
 
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.

Do you mean deallocated like this?
Code:
- (void)dealloc {
	[picturesArray release];
	[super dealloc];
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.