I am trying to load an NSMutableArray with UIImageViews. Everything is going fine with that.
Unfortunately, I have no idea how to use the objects when they are in the mutable array.
Here is some code:
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
NSMutableArray *array = [NSMutableArray new];
[array loadWithObject:(UIImageView *)imageView];
[imageView release];
That kind of sets up what I’ve done. Here’s what I want to do:
[array objectAtIndex:5].center = GCRectMake(0, 0);
but that doesn’t work. How can I do this??
OK, I’ll explain the problems you’re having. The way to do what you are trying to do is the following:
First, there is no method
-[NSMutableArray loadWithObject:]. Likewise, for your example, you don’t really even need a mutable array. Mutable objects have their place, but I usually try to use immutable ones when it makes sense to; as such, I’ve usedNSArray.Next, you never need to typecast objects when you’re adding them to an array. There are a few reasons wherefore your example didn’t work:
You were accessing the sixth (starting at one) object in the array. Was there an instance of
UIImageViewat that index?For some reason, dot-notation for getters and setters only works when the compiler knows the type of the object you’re sending a message to. Since the type of an object that is coming out of an array is not clear at compile-time, you can’t use dot-notation. Instead, just use old-fashioned Objective-C method-sending syntax (“brackets and colons”).
Finally, it’s Core Graphics, not Gore Craphics: hence the prefix is
CG, notGC. Also,-[UIImageView setCenter:]takes aCGPoint, notCGRect. So the function you wanted wasCGPointMake.Best of luck to you! Let me know if this helps clear some things up.