我可以解析数据并查看输出,但是当我调用包含 JSON 的视图时,我无法在表视图中显示它们,应用程序崩溃了
这是我的代码:
在第一行代码中,我们定义了一个宏,它为我们提供了一个后台队列——我喜欢为此设置一个 kBgQueue 快捷方式,这样我可以让我的代码更紧凑。在第二行代码中,我们创建了一个名为 kLatestKivaLoansURL 的宏,它返回一个指向该 URL http://api.kivaws.org/v1/loans/search.json?status=fundraising的 NSURL 。
#define kBgQueue dispatch_get_global_queue(
DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) //1
#define kLatestKivaLoansURL [NSURL URLWithString:
@"http://api.kivaws.org/v1/loans/search.json?status=fundraising"] //2
@interface TeacherViewController ()
@end
@implementation TeacherViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
将 kBGQueue 定义为为我们提供后台队列的宏?
- (void)viewDidLoad
{
[super viewDidLoad];
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL:kLatestKivaLoansURL];
[self performSelectorOnMainThread:@selector(fetchedData:)
withObject:data waitUntilDone:YES];
});
}
@synthesize latestLoans;
@synthesize arrayOfFighterName;
将被调用并将 NSData 实例传递给它。在我们的例子中,JSON 文件相对较小,所以我们将在主线程中的 fetchedData: 中进行解析。如果您正在解析大型 JSON 提要(通常是这种情况),请务必在后台执行此操作。
- (void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData //1
options:kNilOptions
error:&error];
latestLoans = [json objectForKey:@"loans"]; //2
arrayOfFighterName=[[NSMutableArray alloc] init];
//NSLog(@"loans: %@", latestLoans); //3
for( int i = 0; i<[latestLoans count]; i++){
// NSLog(@"%@", [matchListArray objectAtIndex:i]);
arrayOfFighterName[i]=[[latestLoans objectAtIndex:i] objectForKey:@"name"];
// NSLog(@"%@", [arrayOfFighterName objectAtIndex:i]);
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
latestLoans = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
在表格视图中显示结果但不幸的是结果不显示并且应用程序崩溃了
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return [arrayOfFighterName count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"TeacherCell"];
cell.textLabel.text = [arrayOfFighterName objectAtIndex:indexPath.row];
return cell;
// NSLog(@"viewDidLoad is called");
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
@end