HairOfTheDog HairOfTheDog - 1 year ago 110
Objective-C Question

no known instance method for selector 'copy' on protocol conforming to NSCopying and NSObject

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
is of type
and given that
conforms to both
why does it not have a

IGTestClass.h file

#import <Foundation/Foundation.h>

@protocol IGTestClassData<NSCopying, NSObject>
@property (nonatomic) NSString* localId;

@interface IGTestClass : NSObject
id<IGTestClassData> _localData;


IGTestClass.m file

#import "IGTestClass.h"

@implementation IGTestClass

self = [super init];
if (self)
_localData = localData;

return self;

id copiedData = [_localData copy];


Answer Source

Neither protocol NSCopying nor protocol NSObject declares -copy.

NSCopying declares -copyWithZone: only. One solution would be to call [_localData copyWithZone:nil].

Class NSObject declares -copy even though protocol NSObject does not. One solution would be to declare your ivar as type NSObject<IGTestClassData> *.

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