2016-01-12 38 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

相關問題