when to use respondsToSelector in objective-c

后端 未结 4 907
挽巷
挽巷 2020-11-29 18:05
- (void)someMethod
{
    if ( [delegate respondsToSelector:@selector(operationShouldProceed)] )
    {
        if ( [delegate operationShouldProceed] )
        {
             


        
4条回答
  •  星月不相逢
    2020-11-29 18:55

    Just to add to what @kubi said, another time I use it is when a method was added to a pre-existing class in a newer version of the frameworks, but I still need to be backwards-compatible. For example:

    if ([myObject respondsToSelector:@selector(doAwesomeNewThing)]) {
      [myObject doAwesomeNewThing];
    } else {
      [self doOldWorkaroundHackWithObject:myObject];
    }
    

提交回复
热议问题