macos 无法在 Mac 上使用 ARC 使用 RespondsToSelector
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7941051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot use respondsToSelector using ARC on Mac
提问by David
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:
当我respondsToSelector
在 ARC 环境中调用时,收到以下错误消息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;
@end
@interface MTScrollView : NSScrollView
{
}
@property(nonatomic, weak) id<MTScrollViewDelegate>delegate;
@end
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];
}
}
@end
Any suggestions on why I am getting this error?
关于为什么我收到此错误的任何建议?
回答by Jason Harwig
Make the protocol conform to NSObject
使协议符合 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.
否则编译器不会认为该对象会响应 NSObject 之类的消息respondsToSelector
,并且会生成警告。无论哪种方式,它都会在运行时成功。
回答by niket
For Swift this becomes:
对于 Swift,这变成了:
@objc protocol MTScrollViewDelegate: NSObjectProtocol
The NSObject protocol groups methods that are fundamental to all Objective-C objects.
NSObject 协议对所有 Objective-C 对象的基础方法进行分组。
For more information on what NSObjectProtocol is: https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/index.html
有关 NSObjectProtocol 是什么的更多信息:https: //developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/index.html