tags:

views:

185

answers:

2

I wat to do something like this:

if (viewController.mapView) [viewController.mapView someMethod];

However, if mapView is not a class variable, this crashes. How do I check if mapView exists?

+2  A: 

Oops, found it:

if ([vc respondsToSelector:@selector(mapView)]) {

  [[vc mapView] viewWillAppear:YES];

}
Andrew Johnson
You can also use it on synthesized property methods like `setMapView:`
Nick Bedford
or any property with an accessor and setter. @property
pxl
+6  A: 

For ordinary selectors, you can use respondsToSelector:. I'm not certain if this will work for new-style property access (as it appears you are using in this example). To test if a class responds to a given selector, use instancesRespondToSelector:.

dcrosta