【发布时间】:2016-01-12 13:55:13
【问题描述】:
在我的一堂课中,我已从 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
如果您需要我提供其他课程的代码,请不要
犹豫告诉我。
提前致谢
更新
从此改变:
[_taskDays addObject:tmpLIUTaskDay];
到这里:
[_taskDays setObject:d forKey:tmpLIUTaskDay.deadline];
【问题讨论】:
-
为什么把它从数组改成字典?
-
因为使用日期从日历中访问任务会更容易:)
-
那么字典的键是月份的哪一天?我不认为这会让事情变得更容易。您是否也考虑过在同一天完成多项任务?
-
是的,这就是我正在使用的 [_taskDays[datePlace]addDatedTask:d];我也收到了一个错误,因为我不知道如何更改它。
标签: objective-c nsmutablearray nsdictionary