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

时间:2013-05-09 00:53:00

标签: ios runtime-error unrecognized-selector copywithzone

我刚刚实现了我的课程

@interface ExampleNestedTablesViewController ()
{
    NSMutableArray *projectModelArray;
    NSMutableDictionary *sectionContentDictionary;

}

- (void)viewDidLoad
{
    [super viewDidLoad];

    ProjectModel *project1 = [[ProjectModel alloc] init];
    project1.projectName = @"Project 1";

    ProjectModel *project2 = [[ProjectModel alloc] init];
    project2.projectName = @"Project 2";
    if (!projectModelArray)
    {
        projectModelArray = [NSMutableArray arrayWithObjects:project1, project2, nil];
    }

    if (!sectionContentDictionary)
    {
        sectionContentDictionary  = [[NSMutableDictionary alloc] init];

        NSMutableArray *array1     = [NSMutableArray arrayWithObjects:@"Task 1", @"Task 2", nil];
        [sectionContentDictionary setValue:array1 forKey:[projectModelArray objectAtIndex:0]]; // **this line crashed**.

    }
}

这是我的项目模型

@interface ProjectModel : NSObject

typedef enum
{
    ProjectWorking = 0,
    ProjectDelayed,
    ProjectSuspended,

} ProjectStatus;

@property (nonatomic, assign) NSInteger idProject;
@property (nonatomic, strong) NSString* projectName;
@property (nonatomic, strong) NSMutableArray* listStaff;
@property (nonatomic, strong) NSTimer* projectTimer;
@property (nonatomic, assign) ProjectStatus projectStatus;
@property (nonatomic, strong) NSMutableArray* listTask;
@property (nonatomic, assign) NSInteger limitPurchase;
@property (nonatomic, strong) NSDate* limitTime;
@end

输出是: SDNestedTablesExample [1027:c07] - [ProjectModel copyWithZone:]:无法识别的选择器发送到实例0x7562920。 我不知道哪个问题。你能救我吗?

1 个答案:

答案 0 :(得分:3)

查看NSMutableDictionary setObject:forKey:的文档(请注意,您应该使用setObject:forKey:,而不是setValue:forKey:)。注意密钥的预期类型。它必须是id<NSCopying>类型。这意味着密钥必须符合NSCopying协议。

由于您的密钥属于ProjectModel,因为ProjectModel课程没有实施NSCopying协议所需的方法 - copyWithZone:,因此错误正在抱怨

您确定要使用ProjectModel对象作为密钥吗?这样做还意味着除了isEqual:之外,您还需要hashcopyWithZone方法的合理实施。

解决方案是更新您的ProjectModel类,使其符合NSCopying协议并实现copyWithZone:方法。并正确实施isEqual:hash方法。或者将密钥更改为idProject属性(正确包装为NSNumber)。