-[MyClassName copyWithZone:] 无法识别的选择器发送到实例 [英] -[MyClassName copyWithZone:] unrecognized selector sent to instance

查看:15
本文介绍了-[MyClassName copyWithZone:] 无法识别的选择器发送到实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序崩溃了,原因是:

My application crashed with the reason:

-[MyClassName copyWithZone:] 无法识别的选择器发送到实例

-[MyClassName copyWithZone:] unrecognized selector sent to instance

我有两节课.假设 Class1 和 Class2.

I have two classes. Let's say Class1 and Class2.

Class1 看起来像:

Class1 looks like:

@interface Class1 : NSObject {
    NSString *imagemd5CheckSum;
    UIImage *image;
    NSData *fileChunkData;
}

@property (nonatomic, copy)NSString *imagemd5CheckSum;
@property (nonatomic, copy)UIImage *image;
@property (nonatomic, copy)NSData *fileChunkData;

@end

Class1.m

@implementation Class1

@synthesize image;
@synthesize fileChunkData;
@synthesize imagemd5CheckSum;

-(id) init{
    [self setImage:nil];
    [self setFileChunkData:nil];
    [self setImagemd5CheckSum:@""];

    return self;
}

-(void)dealloc{
    [imagemd5CheckSum release];
    [image release];
    [fileChunkData release];

    fileChunkData = nil;
    imagemd5CheckSum = nil;
    image = nil;

    [super dealloc];
}
@end

**

Class2 的样子

**

#import "Class2.h"
@interface Class2 : NSObject {
    Class1 *obj1;
    Class1 *obj2;
    Class1 *obj3;
}

@property (nonatomic, copy)Class1 *obj1;
@property (nonatomic, copy)Class1 *obj2;
@property (nonatomic, copy)Class1 *obj3;

@end

Class2.m

<小时>

@implementation Class2

@synthesize obj1,obj2,obj3;

-(id) init{
    [self setObj1:nil];
    [self setObj2:nil];
    [self setObj3:nil];

    return self;
}

-(void)dealloc{
    [obj1 release];
    [obj2 release];
    [obj3 release];

    obj1 = nil;
    obj2 = nil;
    obj3 = nil;

    [super dealloc];
}
@end

崩溃情况

Class2 *class2 = [[Class2 alloc] init];

Class1 *class1 = [[Class1 alloc] init];

[class1 setImagemd5CheckSum:@"this is md5"];
[class1 setImage:myimage];
[class1 setFileChunkData:myData];

[class2 setObj1:class1]; // This line is crashed..

...

当我调用 [class2 setObj1:class1]; 时,应用程序崩溃了,原因是:

When I called [class2 setObj1:class1];, the application crashed with reason:

-[Class1 copyWithZone:] 无法识别的选择器发送到实例

-[Class1 copyWithZone:] unrecognized selector sent to instance

我该如何解决这个问题?

How can I fix this problem?

推荐答案

你的 -setObj1: 方法被声明为 copy,所以它调用 -copy 在您的 Class1 对象上.-copy 只是调用 -copyWithZone:nil.因此,您要么需要实现 NSCopying 协议(这意味着实现 -copyWithZone:),要么将您的属性从 copy 更改为 retain.

Your -setObj1: method is declared as copy, so it calls -copy on your Class1 object. -copy just calls -copyWithZone:nil. So you either need to implement the NSCopying protocol (which means implementing -copyWithZone:), or change your property from copy to retain.

这篇关于-[MyClassName copyWithZone:] 无法识别的选择器发送到实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆