Facebook JSON 提要 - __NSArrayM insertObject:atIndex:]:对象不能为 nil
Posted
技术标签:
【中文标题】Facebook JSON 提要 - __NSArrayM insertObject:atIndex:]:对象不能为 nil【英文标题】:Facebook JSON feed - __NSArrayM insertObject:atIndex:]: object cannot be nil 【发布时间】:2013-05-01 23:26:48 【问题描述】:我正在尝试将 Facebook 页面 Json Feed 解析为表格视图并关注此Tutorial 我收到此错误:“由于未捕获的异常 'NSInvalidArgumentException' 而终止应用程序,原因:'* -[__NSArrayM insertObject:atIndex:]: object cannot be nil'”
这是我的 tableview.h 中的代码:
#import "FBViewController.h"
@interface FBViewController ()
NSMutableData *fbdata;
NSMutableArray *array;
@end
@implementation FBViewController
- (void)viewDidLoad
[super viewDidLoad];
[[self fbtableview]setDelegate:self];
[[self fbtableview]setDataSource:self];
array = [[NSMutableArray alloc]init];
NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/XXXXXXX/posts?access_token=ACCESS_TOKEN"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
connection = [NSURLConnection connectionWithRequest:request delegate:self];
if (connection)
fbdata=[[NSMutableData alloc]init];
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
[fbdata setLength:0];
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
[fbdata appendData:data];
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
NSLog(@"fail with error");
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:fbdata options:0 error:nil];
NSArray *arraydata = [allDataDictionary objectForKey:@"data"];//objectforkey corresponde ao nome do dicionario
for (NSDictionary *diction in arraydata)
NSString *message =[diction objectForKey:@"message"];
[array addObject:message];
[[self fbtableview]reloadData];
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
return 1;
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return [array count];
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.text = [array objectAtIndex:indexPath.row];
return cell;
@end
谁能帮帮我?
【问题讨论】:
【参考方案1】:问题就在这里,我想是的,
for (NSDictionary *diction in arraydata)
NSString *message =[diction objectForKey:@"message"];
[array addObject:message]; //here some value is nil means you are trying to add a nil object to array that's why app getting crashed.
[[self fbtableview]reloadData];
【讨论】:
我插入了一个 NSLog 来显示我的消息变量,它上面有一个字符串。那会是什么? 我终于找到了问题所在。我插入了: if (message != nil) 来测试它是否不为零,然后添加对象。谢谢 @Miguel Grenho:是的,这就是为什么我保留对该行问题本身的评论,如果有用,请接受我的回答。 完成。感谢您的帮助以上是关于Facebook JSON 提要 - __NSArrayM insertObject:atIndex:]:对象不能为 nil的主要内容,如果未能解决你的问题,请参考以下文章