【问题标题】:Having an issue creating a NSDictionary?创建 NSDictionary 时遇到问题?
【发布时间】:2016-01-31 00:00:46
【问题描述】:

我正在创建一个使用谷歌 API 与谷歌日历链接的日历。我发现了一个很棒的日历,我可以在 Github 上使用。我现在正在将从 google API 收到的 JSON 数据转换为适合来自 GitHub 的日历视图的格式。

我需要存储一个 NSObject,在 NSArray 中,在 NSDictionary 中,在 NSDictionary 中。

NSObject- 一个小型 NSObject,用于存储标题、描述、日期等。

NSArray- 存储所有的 NSObjects(上一行)

NSDictionary(第一个)- 将 NSArray(上一行)存储为对象,并将原始对象的月份存储为键

NSDictionary(第二个)- 存储“月份字典”(上一行)作为对象,原始对象作为键。

截至目前,它会在“年份字典”中添加正确的年份,但不会正确存储其他数据。

这是我的代码,此代码将以不同的方法针对 for 循环中的每个事件运行。

ATEvent *event = [ATEvent new];
event.title = eventGiven[@"summary"];
event.summary = eventGiven[@"description"];
[event compileStartDate:eventGiven[@"start"]];
[event compileEndDate:eventGiven[@"end"]];
event.location = eventGiven[@"location"];

NSCalendar *defaultCalendar = [NSCalendar currentCalendar];
NSDateComponents *components = [defaultCalendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:date]; // Get necessary date components

NSString *month = [NSString stringWithFormat:@"%li",(long)[components month]];
NSString *year = [NSString stringWithFormat:@"%li",(long)[components year]];

NSMutableDictionary *yearDictionary = [[NSMutableDictionary alloc] initWithDictionary:dataByDate];
NSMutableDictionary *monthDictionary = [[NSMutableDictionary alloc] initWithDictionary:yearDictionary[month]];
NSMutableArray *monthArray = [[NSMutableArray alloc] initWithArray:monthDictionary[month]];

[yearDictionary setObject:monthDictionary forKey:year];
[monthDictionary setObject:monthArray forKey:month];
[monthArray addObject:event];

NSLog(@"%@",yearDictionary);

dataByDate = yearDictionary;

谢谢,我花了几个小时盯着看,逻辑地思考这一切,不明白我哪里错了! :|

【问题讨论】:

    标签: objective-c json nested nsarray nsdictionary


    【解决方案1】:

    首先,如果你想建立你的年份和月份字典来存储事件,你不想在每次事件进入时都重新创建它们。你需要创建字典(一年和十二个月的字典我假设)在事件处理循环之外。您还想创建一个空数组来存储事件然后您想将每个事件添加到相应月份和相应年份的数组中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-25
      • 2013-11-10
      相关资源
      最近更新 更多