YouTube V2 API から JSON を解析しようとしましたが、クラッシュしていて、理由がわかりません... Android から iOS を初めて使用するだけです。
コード
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = NSLocalizedString(@"Videos", @"Videos");
if ([self.navigationController.parentViewController respondsToSelector:@selector(revealGesture:)] && [self.navigationController.parentViewController respondsToSelector:@selector(revealToggle:)])
{
UIPanGestureRecognizer *navigationBarPanGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self.navigationController.parentViewController action:@selector(revealGesture:)];
[self.navigationController.navigationBar addGestureRecognizer:navigationBarPanGestureRecognizer];
UIImage *backImage=[UIImage imageNamed:@"NavBarIconLauncher.png"];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:backImage style:UIBarButtonItemStylePlain target:self.navigationController.parentViewController action:@selector(revealToggle:)];
//self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(revealToggle:)];
}
[SVProgressHUD showWithStatus:@"Loading Videos..." maskType:SVProgressHUDMaskTypeClear];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSURL *url = [NSURL URLWithString:@"http://gdata.youtube.com/feeds/api/users/YOUTUBE USERNAME/uploads?v=2&alt=jsonc"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self ];
NSLog(@"JSON REQUESTED");
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return (toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
data = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
[data appendData:theData];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"JSON RECEIVED");
[SVProgressHUD dismiss];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
videos = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
[mainTableView reloadData];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[SVProgressHUD dismiss];
[SVProgressHUD showErrorWithStatus:error.localizedDescription];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[videos valueForKeyPath:@"data.items.title"] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"];
cell.textLabel.numberOfLines = 1;
cell.selectionStyle = UITableViewCellAccessoryDisclosureIndicator;
cell.accessoryType= UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.lineBreakMode = UILineBreakModeTailTruncation;
}
NSDictionary *datas = [videos objectForKey:@"data"];
NSDictionary *items = [datas objectForKey:@"items"];
NSArray *announcements = [items objectForKey:@"title"];
// NSDictionary* announcement = [announcements objectAtIndex:0];
cell.textLabel.text = [[announcements objectAtIndex:indexPath.row]valueForKey:@"title"];
return cell;
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
スタックトレース
2013-03-29 18:35:42.212 SJRC[2818:907] -[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x2025d290
2013-03-29 18:35:42.215 SJRC[2818:907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x2025d290'
*** First throw call stack:
(0x326922a3 0x3a30597f 0x32695e07 0x32694531 0x325ebf68 0x107633 0x344e554d 0x344ca313 0x344e17cf 0x3449d803 0x34247d8b 0x34247929 0x3424885d 0x34248243 0x34248051 0x34247eb1 0x326676cd 0x326659c1 0x32665d17 0x325d8ebd 0x325d8d49 0x361892eb 0x344ee301 0xe52d5 0xdd788)
libc++abi.dylib: terminate called throwing an exception
(lldb)