问题描述
在ARC环境中调用respondsToSelector
时,出现以下错误消息Automatic Reference Counting Issue
No known instance method for selector respondsToSelector:
When I call respondsToSelector
in an ARC environment, I get the following error message Automatic Reference Counting Issue
No known instance method for selector respondsToSelector:
这是标题
#import <AppKit/AppKit.h>
@class MTScrollView;
@protocol MTScrollViewDelegate
-(void)scrollViewDidScroll:(MTScrollView *)scrollView;
@end
@interface MTScrollView : NSScrollView
{
}
@property(nonatomic, weak) id<MTScrollViewDelegate>delegate;
@end
这是实现文件
#import "MTScrollView.h"
@implementation MTScrollView
@synthesize delegate;
- (void)reflectScrolledClipView:(NSClipView *)aClipView
{
[super reflectScrolledClipView:aClipView];
if([delegate respondsToSelector:@selector(scrollViewDidScroll:)])
{
[delegate scrollViewDidScroll:self];
}
}
@end
关于为什么会出现此错误的任何建议?
Any suggestions on why I am getting this error?
推荐答案
使协议符合NSObject
Make the protocol conform to NSObject
@protocol MTScrollViewDelegate <NSObject>
否则,编译器认为该对象不会响应respondsToSelector
之类的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.
这篇关于在Mac上无法通过ARC使用responsToSelector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!