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

xcodeNewbie

macrumors member
Original poster
Jul 1, 2011
65
0
I have an NSMutableArray, called myArray. No matter what I do, the objects in it won't respond to any command. Take a look at this code:
Code:
UIImageView *myView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pic.jpg"]];
myView.frame = CGRectMake(0,0,100,100);
[self.view addSubview:myView];
[myView release];
The above code works perfectly, a UIImageVIew is created in the top left corner of the screen. However, look at this code:
Code:
UIImageView *myView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pic.jpg"]];
myView.frame = CGRectMake(0,0,100,100);
[myArray replaceObjectAtIndex:0 withObject:myView];
[self.view addSubview:[myArray objectAtIndex:0]];
[myView release];
With this code, no ImageVIew appears. I've been having trouble for so long with Arrays, it's like when an object is in an array it becomes invisible and unusable.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Time for some basic debugging. What is the value of myArray when you call replaceObjectAtIndex:withObject? Is it non-nil? Has it been initialized? Etc.

P.S. How and where is myArray defined?
 

RonC

macrumors regular
Oct 18, 2007
108
0
Chicago-area
It was initialized with an object count of 1.

Huh? Do you mean you created it with a capacity of 1? I don't believe that adds ANY objects in the array, rather it sets some internal allocation size for possible addition of objects.

Add the following line of code on either side of your "replaceObjectAtIndex:withObject: method:
Code:
NSLog(@"myArray has %d objects in it.",[myArray count]);

Tell us what that prints.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.