0

In my first file I want to be able to get reference to my second file and change a property that it has, This is what I have. I made a class method to return the reference but the problem is I get a warning in the method, on top of that when I do the if statement it doesnt seem to run.

First File that needs reference, calls class method to get reference

-(void) updateSplitViewDetail{

id detail =  (MapViewController*) [MapViewController returnReference];
NSLog(@"%@", [detail class]); //This post MAPVIEWCONTROLLER

//This fails so I cant access the methods inside.
if ([detail isKindOfClass:[MapViewController class]]) {
    MapViewController *mapVC = (MapViewController*) detail;
    mapVC.delegate = self;
    mapVC.annotations = [self mapAnnotations];
}

}

 (void)viewDidLoad
  {
[super viewDidLoad];    
[self updateSplitViewDetail]; //Error may be here?
  }

Second File that I want reference to, returns reference using self.

- (void)viewDidLoad
{

NSLog(@"%@", [self class]);

[super viewDidLoad];
self.mapView.delegate = self;
// Do any additional setup after loading the view.
}

+(MapViewController*) returnReference{
//I also get an incompatible pointer return warning here?
return self;
}
Terrel Gibson
  • 481
  • 1
  • 6
  • 21

2 Answers2

1
+(MapViewController*) returnReference {
    //I also get an incompatible pointer return warning here?
    return self;
}

You get a warning because this is a class method (see the +) therefore this is of type Class not of type (MapViewController*). It refers to the MapViewController class not to an instance of that class. And the pointer you are returning is the class itself, not an instance. This is why the test fails and you cannot call the instance methods in the other code.

You probably want to instantiate the class and return the instance instead.

Analog File
  • 5,280
  • 20
  • 23
1

As @AnalogFile mentioned, inside a class method, self is the class object itself, so detail is the MapViewController class object. i.e. it is true that detail == [MapViewController class].

[detail class] also evaluates to the MapViewController class object, because calling the class method on a class object calls +class, which returns the class object itself (+class is basically an identity method, returning the thing it was called on; unlike -class. In fact, your +returnReference method is basically a re-implementation of the +class method.). So in fact, detail == [detail class] is true.

[detail isKindOfClass:[MapViewController class]], on the other hand, fails, because it calls -isKindOfClass: (there is no separate +isKindOfClass:), which tests if the object is an instance of that class, and your class object is not an instance of itself (a class object is an instance of its metaclass, which follows the inheritance chain to the metaclass of the root class, which then inherits from the root class itself).

newacct
  • 119,665
  • 29
  • 163
  • 224
  • so if I created a mapviewcontroller property in my second file and return it by making return reference and instance variable, and then call it using an instance of mapviewcontroller in the first file should it work> – Terrel Gibson Aug 15 '12 at 05:47
  • @TerrelGibson: I don't really understand what you're saying. But `+returnReference` being a class method, it is a method only on the class. It is not a method of instances. If you try to call `returnReference` on an instance it won't work because there is no `-returnReference`. – newacct Aug 15 '12 at 09:38
  • if I change +returnReference to -ReturnReference? – Terrel Gibson Aug 15 '12 at 18:12
  • @TerrelGibson: I guess you can do that. (though a method that returns the thing it's called on is still pretty useless) – newacct Aug 15 '12 at 18:20