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?
The hasOwnProperty() method will check if an object contains a direct property and will return true or false if it exists or not. The hasOwnProperty() method will only return true for direct properties and not inherited properties from the prototype chain.
The first way is to invoke object. hasOwnProperty(propName) . The method returns true if the propName exists inside object , and false otherwise. hasOwnProperty() searches only within the own properties of the object.
The Objective-C declared properties feature provides a simple way to declare and implement an object's accessor methods.
[yourObject isKindOfClass:[a class]] // Returns a Boolean value that indicates whether the receiver is an instance of // given class or an instance of any class that inherits from that class.
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:
.
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]; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With