2013-05-09 161 views
2

我只是實現我的課[MyClassName 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。 我不知道哪個問題。你可以幫我嗎 ?

回答

3

查看NSMutableDictionary setObject:forKey:的文檔(請注意,您應該使用setObject:forKey:而不是setValue:forKey:)。注意鍵的預期類型。它必須是id<NSCopying>。這意味着密鑰必須符合NSCopying協議。

因爲你的鑰匙是ProjectModel型的,錯誤是抱怨,因爲您的ProjectModel類未實現所需的NSCopying協議的方法 - copyWithZone:

您確定要使用ProjectModel對象作爲關鍵嗎?這樣做也意味着除了copyWithZone之外,您還需要一個合理的isEqual:hash方法。

解決方案是更新您的ProjectModel類,使其符合NSCopying協議並實現copyWithZone:方法。並正確實施isEqual:hash方法。或者將密鑰更改爲idProject屬性(正確包裝爲NSNumber)。

相關問題