Accessing Array from Another Class

Discussion in 'iOS Programming' started by RagingGoat, Jun 20, 2013.

  1. RagingGoat macrumors 6502

    Jun 21, 2010
    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?
    RSFM *rsfm = [[RSFM alloc]init];
    NSArray *annotations = [rsfm.worldView.annotations mutableCopy];
    NSLog(@"Annotations Array: %@ Count: %d", annotations, [annotations count]);
  2. chown33, Jun 20, 2013
    Last edited: Jun 20, 2013

    chown33 macrumors 604

    Aug 9, 2009
    descending into the Maelström
    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:
    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.
  3. ArtOfWarfare macrumors G3


    Nov 26, 2007
    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
    id annotations = [rsfm.worldView.annotations mutableCopy];
    And the actual instance at annotations would be an NSMutableArray that responds to all of those methods.
  4. Duncan C macrumors 6502a

    Duncan C

    Jan 21, 2008
    Northern Virginia
    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.
  5. chown33 macrumors 604

    Aug 9, 2009
    descending into the Maelström
    No. Yes. 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.
  6. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    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.

Share This Page