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

RagingGoat

macrumors 6502
Original poster
Jun 21, 2010
307
15
I'm trying to use the annotations array from my map view in another class but the copy I've made in the new class is null and I'm not sure why. In the class with the map view it has 103 annotations. Any ideas?
Code:
RSFM *rsfm = [[RSFM alloc]init];
NSArray *annotations = [rsfm.worldView.annotations mutableCopy];
NSLog(@"Annotations Array: %@ Count: %d", annotations, [annotations count]);
 
Does the RSFM class itself have a worldView.annotations object? Or does each instance of RSFM have a worldView.annotations object?

I suspect you don't completely understand the difference between a class and an instance of a class.

By the way:
Code:
NSArray *annotations = [rsfm.worldView.annotations mutableCopy];
Makes a mutableCopy (NSMutableArray) but then stores that copy into a variable of immutable type (NSArray). So perhaps you don't understand the type system well enough, either.
 
Last edited:
By the way:
Code:
NSArray *annotations = [rsfm.worldView.annotations mutableCopy];
Makes a mutableCopy (NSMutableArray) but then stores that copy into a variable of immutable type (NSArray). So perhaps you don't understand the type system well enough, either.

Assigning it to a pointer of type NSArray doesn't actually change the type of the instance, does it? It's still going to be mutable, is it not? Isn't NSMutableArray a subclass of NSArray?

Couldn't you do
Code:
id annotations = [rsfm.worldView.annotations mutableCopy];

And the actual instance at annotations would be an NSMutableArray that responds to all of those methods.
 
I'm trying to use the annotations array from my map view in another class but the copy I've made in the new class is null and I'm not sure why. In the class with the map view it has 103 annotations. Any ideas?
Code:
RSFM *rsfm = [[RSFM alloc]init];
NSArray *annotations = [rsfm.worldView.annotations mutableCopy];
NSLog(@"Annotations Array: %@ Count: %d", annotations, [annotations count]);

Several things.

I'm assuming that your RSFM class is a view controller class?

If it is, then I'm further assuming that the RSFM class's worldView property is a map view object (MKMapView)?

You are breaking one of the big rules of MVC. Don't manipulate another view controller's view objects directly. Treat them as the private property of the other view controller.

In this case, you are failing because a view controller doesn't create it's view hierarchy until the first time it's displayed on the screen (or you try to reference self.view in your code.) Before that, all a view controller's outlets are nil.

You should add a loadViews method to your RSFM class that causes it to load it's view hierarchy. It can be as simple as this:

- (void) loadViews;
{
UIView *myView = self.view; //Force the views to load
}

Next, you need to add code to your view controller that causes it to install whatever annotations it should load into it's map view. How you do that is app-specific.

Finally, you should add a myMapAnnotations property to your RSFM class. When you build your array of annotations and install it in your map view, also save it in your myMapAnnotations property.

Then, in your other object, read the myMapAnnotations property instead of trying to fetch the annotations from the RSFM object's map view.
 
Assigning it to a pointer of type NSArray doesn't actually change the type of the instance, does it? It's still going to be mutable, is it not? Isn't NSMutableArray a subclass of NSArray?
No. Yes. Yes.


Couldn't you do
Code:
id annotations = [rsfm.worldView.annotations mutableCopy];

And the actual instance at annotations would be an NSMutableArray that responds to all of those methods.
Yes.

The difference is that when the declared type is 'id', the compiler does no compile-time checks that the receiver has a declared method. Thus there are no warnings that the object may not respond to a given message.
 
Code:
RSFM *rsfm = [[RSFM alloc]init];
NSArray *annotations = [rsfm.worldView.annotations mutableCopy];

How does worldView and its annotations get assigned a value between the first and second line here? Keep in mind that the first line is creating an entirely new instance of RSFM and not referencing any previous instance.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.