解析iOS错误中的JSON图像网址

时间:2013-10-21 11:54:36

标签: ios json xcode uitableview

我正在尝试解析一个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"
    }
]

我做错了什么?

3 个答案:

答案 0 :(得分:2)

我认为你必须检查属性instaImage。我认为您正在访问尚未在nib中定义的属性。

答案 1 :(得分:1)

拳头尝试调试这个。从崩溃日志中我认为你对自定义单元格做错了。首先检查PictureJSON的连接。并检查您的文件所有者它应该是您的自定义单元格。您可以使用断点确认这一点。如果在创建单元格时出现错误,则上面是解决方案。

答案 2 :(得分:0)

我认为问题不在于您的JSON序列化,因为您的static NSString *simpleTableIdentifier = @"PictureJSON";

导致您的日志崩溃

请确保您已为自定义单元格指定了此标识符!