David David - 1 year ago 111
Objective-C Question

Cannot use respondsToSelector using ARC on Mac

When I call

in an ARC environment, I get the following error message
Automatic Reference Counting Issue
No known instance method for selector respondsToSelector:

This is the header

#import <AppKit/AppKit.h>

@class MTScrollView;

@protocol MTScrollViewDelegate
-(void)scrollViewDidScroll:(MTScrollView *)scrollView;

@interface MTScrollView : NSScrollView


@property(nonatomic, weak) id<MTScrollViewDelegate>delegate;


This is the implementation file

#import "MTScrollView.h"

@implementation MTScrollView

@synthesize delegate;

- (void)reflectScrolledClipView:(NSClipView *)aClipView
[super reflectScrolledClipView:aClipView];

if([delegate respondsToSelector:@selector(scrollViewDidScroll:)])
[delegate scrollViewDidScroll:self];


Any suggestions on why I am getting this error?

Answer Source

Make the protocol conform to NSObject

@protocol MTScrollViewDelegate <NSObject>

Otherwise the compiler doesn't think that the object will respond to NSObject messages like respondsToSelector, and will generate a warning. It will succeed at runtime without issues either way.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download