When I compile the code below the line id copiedData = [_localData copy]; results in the compiler error "no known instance method for selector 'copy'." Given that _localData is of type id<IGTestClassData> and given that IGTestClassData conforms to both NSCopying and NSObject why does it not have a copy method?
IGTestClass.h file
#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 file
#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
Neither protocol
NSCopyingnor protocolNSObjectdeclares-copy.NSCopyingdeclares-copyWithZone:only. One solution would be to call[_localData copyWithZone:nil].Class
NSObjectdeclares-copyeven though protocolNSObjectdoes not. One solution would be to declare your ivar as typeNSObject<IGTestClassData> *.