PDA

View Full Version : Objects in Array Won't respond




xcodeNewbie
Jul 19, 2011, 02:15 PM
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:
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:
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
Jul 19, 2011, 02:25 PM
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?

xcodeNewbie
Jul 19, 2011, 02:27 PM
It was initialized with an object count of 1.

RonC
Jul 19, 2011, 02:41 PM
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:
NSLog(@"myArray has %d objects in it.",[myArray count]);

Tell us what that prints.