【问题标题】:-[MyClassName copyWithZone:] unrecognized selector sent to instance-[MyClassName copyWithZone:] 无法识别的选择器发送到实例
【发布时间】:2012-07-08 15:52:46
【问题描述】:

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

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

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

Class1 看起来像:

Class1.h

@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 的样子

**

Class2.h


#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];时,应用程序崩溃了,原因是:

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

我该如何解决这个问题?

【问题讨论】:

    标签: iphone objective-c class crash copy


    【解决方案1】:

    您的-setObj1: 方法被声明为copy,因此它在您的Class1 对象上调用-copy-copy 只需调用 -copyWithZone:nil。因此,您要么需要实现NSCopying 协议(即实现-copyWithZone:),要么将您的属性从copy 更改为retain

    【讨论】:

      【解决方案2】:

      要让您的班级响应copyWithZone:,您必须在班级中实现NSCopying 协议。而且你必须重写copyWithZone: 方法。

      例如:

      首先你必须在你的接口声明中实现 NSCopying 协议。

      @interface MyObject : NSObject <NSCopying>
      

      然后重写copyWithZone方法,如,

      - (id)copyWithZone:(NSZone *)zone
      {
          id copy = [[[self class] alloc] init];
      
          if (copy)
          {
              // Copy NSObject subclasses
              [copy setVendorID:[[self.vendorID copyWithZone:zone] autorelease]];
              [copy setAvailableCars:[[self.availableCars copyWithZone:zone] autorelease]];
      
              // Set primitives
              [copy setAtAirport:self.atAirport];
          }
      
          return copy;
      }
      

      如果这对你有帮助,我很高兴。

      (Reference)

      【讨论】:

      • 我认为在这个讨论中必须提到 zone 参数是deprecated这一事实
      • 嘿,伙计.. 您是否从this 答案中复制了您的代码?这个机场和汽车是怎么回事?
      • 查看添加的参考。您将了解机场和汽车的全部信息。
      猜你喜欢
      • 2014-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-09
      • 1970-01-01
      • 2019-08-28
      相关资源
      最近更新 更多