我有点难过,由于某种原因,以下代码导致我的应用程序在真正的 iPhone 上崩溃,尽管它在模拟器中运行良好,它所做的一切都是抓取一些 json 并将其放在列表视图中,有人知道吗为什么它总是崩溃?任何帮助是极大的赞赏!
--------SecondViewController.m------
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self fetchPrices];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)fetchPrices
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData* data = [NSData dataWithContentsOfURL:
[NSURL URLWithString: @"http://url.php"]];
NSError* error;
prices = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
});
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return prices.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"PriceCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *price = [prices objectAtIndex:indexPath.row];
NSString *text = [price objectForKey:@"name"];
NSString *name = [price objectForKey:@"price"];
cell.textLabel.text = text;
cell.detailTextLabel.text = [NSString stringWithFormat:@"Price: %@", name];
return cell;
}
@end
-----SecondViewController.h----
#import <UIKit/UIKit.h>
@interface SecondViewController : UITableViewController {
NSArray *prices;
}
- (void)fetchPrices;
@end
---- 崩溃日志 ---- http://pastebin.com/cnf6L7Jf