在我的一個類中,我從NSMutableArray
更改爲NSMutableDictionary
。
之前,我訪問這樣的其他類對象:當從NSMutableArray更改爲NSMutableDictionary時訪問對象
tmpDeadline = [_taskDays[i] deadline]; //deadline is a object of another class
和訪問方法是這樣的:
[_taskDays[datePlace]addDatedTask:d]; //addDatedTask is a method in another class
但現在,因爲我得到了很多的錯誤,我不能再這樣下去了,我真的不知道如何處理。
我所知道的是,我希望使用其他類的「截止日期」作爲鍵和類的實例作爲對象。
這裏是給我的問題的評論ERROR代碼(我已經給出了代碼:
#import "LIUTaskCalendar.h"
#import "LIUTaskDay.h"
#import "LIUDatedTask.h"
@interface LIUTaskCalendar()
{
NSMutableDictionary *_taskDays;
}
@end
@implementation LIUTaskCalendar
- (void)addDatedTasks:(LIUDatedTask *)d {
if (!_taskDays) {
_taskDays = [[NSMutableDictionary alloc] init];
}
NSInteger length = [_taskDays count];
NSDate *tmpDeadline;
NSDate *tmpDueDate;
NSInteger dateExist = 0;
NSInteger datePlace = 0;
NSDate *tmp;
for (int i = 0; i < length; i++) {
tmpDueDate = d.dueDate;
tmpDeadline = [_taskDays[i] deadline]; //*ERROR*
if ([tmpDueDate compare:tmpDeadline] == NSOrderedAscending) {
continue;
} else if ([tmpDueDate compare:tmpDeadline] == NSOrderedDescending) {
continue;
} else {
dateExist = 1;
datePlace = i;
break;
}
}
if (dateExist == 1) {
[_taskDays[datePlace]addDatedTask:d]; //*ERROR*
} else {
LIUTaskDay *tmpLIUTaskDay = [[LIUTaskDay alloc]init];
[tmpLIUTaskDay addDatedTask:d];
tmpLIUTaskDay.deadline = d.dueDate;
//[_taskDays setObject:d forKey:tmpLIUTaskDay.deadline];
[_taskDays addObject:tmpLIUTaskDay]; //*ERROR*
}
}
- (void)removeTaskDay:(NSDate *)date {
NSDate *tmpDeadline;
NSDate *tmpDeleteDate;
NSInteger dateExist = 0;
NSDate *dateDelete;
NSInteger length = [_taskDays count];
for (int i = 0; i < length; i++) {
tmpDeleteDate = date;
tmpDeadline = [_taskDays[i] deadline]; //*ERROR*
if ([tmpDeleteDate compare:tmpDeadline] == NSOrderedAscending) {
continue;
} else if ([tmpDeleteDate compare:tmpDeadline] == NSOrderedDescending) {
continue;
} else {
dateExist = 1;
break;
}
}
if (dateExist == 1) {
//[_taskDays removeObjectForKey:dateDelete];
[_taskDays removeObjectAtIndex:dateDelete]; //*ERROR*
} else {
return;
}
}
@end
如果你需要我可以提供代碼爲其他類的話就不要
毫不猶豫地告訴我。
在此先感謝
UPDATE
從此改變:
[_taskDays addObject:tmpLIUTaskDay];
要這樣:
[_taskDays setObject:d forKey:tmpLIUTaskDay.deadline];
你爲什麼從陣列改變它的字典嗎? – trojanfoe
因爲使用日期可以更容易地從日曆中訪問任務:) – MrNaitX
那麼字典的關鍵是每月的哪一天?我認爲這不會讓事情變得更容易。你有沒有在同一天考慮多項任務? – trojanfoe