私はデモ目的でアプリを作成しており、JSON データが取り込まれている uitableview があり、正常に動作します。sessionDate に基づいてテーブルにセクションを作成したいのですが、以下のエラーでアプリがクラッシュします。
キャッチされていない例外 'NSInvalidArgumentException' が原因でアプリを終了しています。理由: '-[Sessions objectForKey:]: 認識されないセレクターがインスタンス 0xac2b460 に送信されました'
JSON を解析するためのコードは次のとおりです。
-(void) retrieveData
{
NSURL * url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"sessions-json.json" ofType:nil]];
NSData * data = [NSData dataWithContentsOfURL:url];
json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
//Set up our speakers array
sessionsArray = [[NSMutableArray alloc] init];
for (int i = 0; i < json.count; i++) {
//create sessions object
NSString * sID = [[json objectAtIndex:i] objectForKey:@"id"];
NSString * sStatus = [[json objectAtIndex:i] objectForKey:@"sessionStatus"];
NSString * sDay = [[json objectAtIndex:i] objectForKey:@"sessionDay"];
NSString * sDate = [[json objectAtIndex:i] objectForKey:@"sessionDate"];
NSString * sTime = [[json objectAtIndex:i] objectForKey:@"sessionTime"];
NSString * sName = [[json objectAtIndex:i] objectForKey:@"sessionName"];
NSString * sSpeaker1 = [[json objectAtIndex:i] objectForKey:@"sessionSpeaker1"];
NSString * sSpeaker1Company = [[json objectAtIndex:i] objectForKey:@"speaker1Company"];
NSString * sSpeaker2 = [[json objectAtIndex:i] objectForKey:@"sessionSpeaker2"];
NSString * sSpeaker2Company = [[json objectAtIndex:i] objectForKey:@"speaker2Company"];
NSString * sSpeaker3 = [[json objectAtIndex:i] objectForKey:@"sessionSpeaker3"];
NSString * sSpeaker3Company = [[json objectAtIndex:i] objectForKey:@"speaker3Company"];
NSString * sSpeaker4 = [[json objectAtIndex:i] objectForKey:@"sessionSpeaker4"];
NSString * sSpeaker4Company = [[json objectAtIndex:i] objectForKey:@"speaker4Company"];
NSString * sSpeaker5 = [[json objectAtIndex:i] objectForKey:@"sessionSpeaker5"];
NSString * sSpeaker5Company = [[json objectAtIndex:i] objectForKey:@"speaker5Company"];
NSString * sSpeaker6 = [[json objectAtIndex:i] objectForKey:@"sessionSpeaker6"];
NSString * sSpeaker6Company = [[json objectAtIndex:i] objectForKey:@"speaker6Company"];
NSString * sDesc = [[json objectAtIndex:i] objectForKey:@"sessionDesc"];
NSString * sITSCECS = [[json objectAtIndex:i] objectForKey:@"ITSCECS"];
NSString * sSessionID = [[json objectAtIndex:i] objectForKey:@"sessionID"];
Sessions * mySessions = [[Sessions alloc] initWithID:sID andSessionStatus:sStatus andSessionDay:sDay andSessionDate:sDate andSessionTime:sTime andSessionName:sName andSessionSpeaker1:sSpeaker1 andSpeaker1Company:sSpeaker1Company andSessionSpeaker2:sSpeaker2 andSpeaker2Company:sSpeaker2Company andSessionSpeaker3:sSpeaker3 andSpeaker3Company:sSpeaker3Company andSessionSpeaker4:sSpeaker4 andSpeaker4Company:sSpeaker4Company andSessionSpeaker5:sSpeaker5 andSpeaker5Company:sSpeaker5Company andSessionSpeaker6:sSpeaker6 andSpeaker6Company:sSpeaker6Company andSessionDesc:sDesc andITSCECS:sITSCECS andSessionID:sSessionID];
//Add our sessions object to our sessionsArray
[sessionsArray addObject:mySessions];
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"sessionDate" ascending:YES];
[sessionsArray sortUsingDescriptors:[NSArray arrayWithObject:sort]];
}
[self.myTableView reloadData];
}
numberOfSectionsInTableView のコードは次のとおりです。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
// Return the number of sections.
//return 1;
if (tableView == self.myTableView) {
return self.sessionsArray.count;
}
else
{
[self searchThroughData];
return self.results.count;
}
}
numberOfRowsInSection のコードは次のとおりです。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
//return sessionsArray.count;
if (tableView == self.myTableView) {
return self.sessionsArray.count;
}
else
{
[self searchThroughData];
return self.results.count;
}
}
titleForHeaderInSection のコードは次のとおりです。
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [[sessionsArray objectAtIndex:section] objectForKey:@"sessionDate"];
}
私は Objective C を使って数か月しか経っていないので、基本的なことを見落としている可能性があります。助けてくれてありがとう。