2016-01-12 37 views
-1

在我的一个类中,我从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]; 
+0

你为什么从阵列改变它的字典吗? – trojanfoe

+0

因为使用日期可以更容易地从日历中访问任务:) – MrNaitX

+0

那么字典的关键是每月的哪一天?我认为这不会让事情变得更容易。你有没有在同一天考虑多项任务? – trojanfoe

回答

0

taskDays是字典,当你使用它,就好像它是在

tmpDeadline = [_taskDays[i] deadline]; //*ERROR* 

也在此数组_taskDays[i]

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

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

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

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

将尝试这些事情,但我如何使它在这工作: [_taskDays [datePlace] addDatedTask:d]; 在这里,我需要从另一个类访问方法 – MrNaitX

+0

我看过你的其他评论,你不能有两次相同的密钥。你会失去第一个价值。你必须知道KVP/Dictionary。还有另外一个问题......“如何访问另一个类的方法”。创建一个类的实例并使用它。如果你需要相同的一组值,那么你需要有一个单独的类。 –

+0

密钥是每月的每一天(NSDate),并在每一天可以有多个任务。 你能告诉我一个如何从字典中访问另一个类的方法的例子吗? – MrNaitX

相关问题