从NSMutableArray更改为NSMutableDictionary时访问对象

时间:2016-01-12 13:55:13

标签: objective-c nsmutablearray nsdictionary

在我的一个课程中,我已从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];

1 个答案:

答案 0 :(得分:0)

taskDays是字典,而您正在使用它,就好像它是

中的数组_taskDays[i]一样
tmpDeadline = [_taskDays[i] deadline]; //*ERROR*

也在这里:

[_taskDays addObject:tmpLIUTaskDay]; //*ERROR*

要从字典中提取或添加新的键值对,您应该这样做:

 tmpDeadline = [_taskDays[@"taskDay"] deadline];

[_taskDays addObject:addObject:tmpLIUTaskDay forKey:@"taskDay"];