54

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?

Andrew Johnson
  • 13,108
  • 13
  • 75
  • 116

4 Answers4

50

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
  • 26,009
  • 8
  • 71
  • 83
  • 4
    Property getter is a method with a name that matches the property and no arguments (i. e. no trailing colon in the signature). – Seva Alekseyev Jul 16 '12 at 20:47
  • 2
    Wow, Objective-C is not one to be concise is it? – devios1 Mar 07 '13 at 00:28
  • 2
    @chaiguy: Objective-C is the furthest from concise as possible. Here's proof: [Longest Objective-C Method](http://stackoverflow.com/a/15152232/2783780). – JVillella Sep 03 '14 at 20:23
38

Also, As Jason poninted out here, you can also use NSSelectorFromString to dynamically check at runtime. E.g.

if ([self respondsToSelector:NSSelectorFromString(elementName)]) 
{
    [self setValue:elementInnerText forKey:elementName];
}
Community
  • 1
  • 1
Robert
  • 37,670
  • 37
  • 171
  • 213
  • I don't think this will work as you intend it to... As stated by the NSSelectorFromString docs: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html#//apple_ref/c/func/NSSelectorFromString "Note, therefore, that if the selector does not exist it is registered and the newly-registered selector is returned." Thus this `if` check will always evaluate to true. – Stunner Feb 06 '14 at 00:15
  • 4
    @Stunner - Might have misunderstood but the docs seem to suggest that `NSSelectorFromString` always returns a SEL no matter if its implemented. This is not a problem since `respondsToSelector:` does the actual checking we are interested in. – Robert Feb 06 '14 at 00:19
  • @Robert It's more than that. It means that if that particular selector does not exist *anywhere* in the program or the system, it will create a SEL for it. This is useful if you dynamically create methods based on user input. OTOH it is also a great way to get an app to use up all its memory if you wanna DoS it. – uliwitness Aug 28 '15 at 23:38
  • See http://stackoverflow.com/questions/4950806/how-to-test-property-existence-and-type-based-on-nsstring-typed-key - you should check for the setter string `setMyProperty:` (note the : as well) – brandonscript Sep 23 '15 at 04:53
32

Oops, found it:

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

  [[vc mapView] viewWillAppear:YES];

}
Andrew Johnson
  • 13,108
  • 13
  • 75
  • 116
  • 2
    You can also use it on synthesized property methods like `setMapView:` – Nick Bedford Oct 08 '09 at 01:50
  • 1
    or any property with an accessor and setter. @property & @synthesize does most the work for you in most cases. what i'm not sure about is if your getter isn't standard, for example @property (getter=notStandardGetter) NSString *aString; – pxl Oct 08 '09 at 08:58
0

Here is more than you asked for but a category I have found useful to generically handle NSObject properties:

http://www.whynotsometime.com/Why_Not_Sometime/Category_Enhancing_NSObject.html

SpareTime
  • 129
  • 2
  • 3