在 iOS 错误中解析 JSON 图像 url
Posted
技术标签:
【中文标题】在 iOS 错误中解析 JSON 图像 url【英文标题】:Parsing JSON image urls in iOS error 【发布时间】:2013-10-21 11:54:36 【问题描述】:我正在尝试解析一个 JSON 文件,其中包含一些图像链接以及一些标题和时间。
这是我的代码:
#import "PicturesViewController.h"
#import "DemoViewController.h"
#import "SecondViewController.h"
#import "AppDelegate.h"
#import "RNBlurModalView.h"
#import "PictureJSON.h"
#import "HMSegmentedControl.h"
@interface PicturesViewController ()
NSInteger refreshIndex;
NSArray *images;
@end
@implementation PicturesViewController
- (void)viewDidLoad
HMSegmentedControl *segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"Instagram", @"Hashtags", @"Facebook"]];
[segmentedControl setFrame:CGRectMake(10, 10, 300, 60)];
[segmentedControl addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segmentedControl];
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Menu" style:UIBarButtonItemStyleBordered target:self action:@selector(showMenu)];
UIPanGestureRecognizer *gestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandler:)];
[self.view addGestureRecognizer:gestureRecognizer];
[self issueLoadRequest];
- (void)swipeHandler:(UIPanGestureRecognizer *)sender
[[self sideMenu] showFromPanGesture:sender];
- (void)segmentedControlChangedValue:(HMSegmentedControl *)segmentedControl1
[self issueLoadRequest];
- (void)segmentedControlSelectedIndexChanged:(id)sender
[self issueLoadRequest];
#pragma mark -
#pragma mark Button actions
- (void)showMenu
[[self sideMenu] show];
#pragma mark - Table view data source
- (void)issueLoadRequest
// Dispatch this block asynchronosly. The block gets JSON data from the specified URL and performs the proper selector when done.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://my-site/pictureparse.php?name=Name"]];
[self performSelectorOnMainThread:@selector(receiveData:) withObject:data waitUntilDone:YES];
);
- (void)receiveData:(NSData *)data
// When we have the data, we serialize it into native cocoa objects. (The outermost element from twitter is
// going to be an array. I JUST KNOW THIS. Reload the tableview once we have the data.
self.tweets = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
[self.myTableView reloadData];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return self.tweets.count;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *simpleTableIdentifier = @"PictureJSON";
PictureJSON *cell = (PictureJSON *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PictureJSON" owner:self options:nil];
cell = [nib objectAtIndex:0];
// The element in the array is going to be a dictionary. I JUST KNOW THIS. The key for the tweet is "text".
NSDictionary *tweet = [self.tweets objectAtIndex:indexPath.row];
NSLog(@"%@", [cell class]);
cell.instaImage = [tweet objectForKey:@"link"];
cell.titleLabel.text = [tweet objectForKey:@"title"];
cell.timeLabel.text = [tweet objectForKey:@"published"];
return cell;
但是当我启动我的应用程序时,我收到了这个错误:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<PicturesViewController 0x758bf70> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key instaImage.'
我的 JSON 文件如下所示:
[
"link": "http://link.com/picture.jpg",
"title": "title",
"published": "0:12 PM 21/10"
,
"link": "http://link.com/picture.jpg",
"title": "title",
"published": "0:09 AM 21/10"
]
我做错了什么?
【问题讨论】:
【参考方案1】:我认为您必须检查属性 instaImage。我认为您正在访问您尚未在 nib 中定义的属性。
【讨论】:
如果单元格不是单元格,那么单元格的其他两个属性也应该抛出异常。【参考方案2】:拳头尝试调试这个。从崩溃日志中,我认为您对自定义单元做错了。首先检查 PictureJSON 的连接。并检查您的文件所有者,它应该是您的自定义单元格。您可以使用 breakpoint 确认这一点。如果您在创建单元格时遇到错误,那么上面就是解决方案。
【讨论】:
为什么要投反对票?至少花点力气说明原因。【参考方案3】:我认为问题不在于您的 JSON 序列化,从您的日志来看,这次崩溃是因为您的 static NSString *simpleTableIdentifier = @"PictureJSON";
确保您已为您的自定义单元格分配了此标识符!
【讨论】:
这不是问题,你可以使用任何字符串作为标识符。 他正在使用 [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; ,如果没有任何具有此标识符的单元格,它将崩溃以上是关于在 iOS 错误中解析 JSON 图像 url的主要内容,如果未能解决你的问题,请参考以下文章