当我编译下面的代码时,id copiedData = [_localData copy];
会导致编译器错误“选择器'复制'没有已知的实例方法”。鉴于它_localData
是类型id<IGTestClassData>
并且IGTestClassData
符合两者NSCopying
,NSObject
为什么它没有copy
方法?
IGTestClass.h 文件
#import <Foundation/Foundation.h>
@protocol IGTestClassData<NSCopying, NSObject>
@property (nonatomic) NSString* localId;
@end
@interface IGTestClass : NSObject
{
@protected
id<IGTestClassData> _localData;
}
-(void)doTest;
@end
IGTestClass.m 文件
#import "IGTestClass.h"
@implementation IGTestClass
-(instancetype)initWithLocalData:(id<IGTestClassData>)localData
{
self = [super init];
if (self)
{
_localData = localData;
}
return self;
}
-(void)doTest
{
id copiedData = [_localData copy];
}
@end