【发布时间】: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