gpt4 book ai didi

objective-c - 在符合 NSCopying 和 NSObject 的协议(protocol)上,选择器 'copy' 没有已知的实例方法

转载 作者:行者123 更新时间:2023-12-02 01:07:09 24 4
gpt4 key购买 nike

当我编译 id copiedData = [_localData copy]; 行下方的代码时导致编译器错误“选择器‘复制’没有已知的实例方法”。鉴于 _localData类型为 id<IGTestClassData>鉴于 IGTestClassData符合 NSCopyingNSObject为什么它没有 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

最佳答案

这两个协议(protocol)NSCopying也不是协议(protocol) NSObject声明 -copy .

NSCopying声明 -copyWithZone:仅有的。一种解决方案是调用 [_localData copyWithZone:nil] .

NSObject声明 -copy即使协议(protocol) NSObject才不是。一种解决方案是将您的 ivar 声明为类型 NSObject<IGTestClassData> * .

关于objective-c - 在符合 NSCopying 和 NSObject 的协议(protocol)上,选择器 'copy' 没有已知的实例方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21199174/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com