2

我刚刚实施了我的课程

@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:]: unrecognized selector sent to instance 0x7562920. 我不知道是哪个问题。你能帮助我吗 ?

4

1 回答 1

3

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

由于您的键是 type ProjectModel,因此错误是抱怨,因为您的ProjectModel类没有实现NSCopying协议所需的方法 - copyWithZone:

您确定要使用ProjectModel对象作为键吗?isEqual:这样做也意味着hash除了copyWithZone.

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

于 2013-05-09T01:01:33.560 回答