In Objective C, can you check if an object has a particular property or message?

前端 未结 4 1391
既然无缘
既然无缘 2020-12-24 10:47

I wat to do something like this:

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

However, if mapView is not a class variab

4条回答
  •  [愿得一人]
    2020-12-24 11:26

    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];
    }
    

提交回复
热议问题