0

I'm trying to parse a JSON file containing some links to images and some titles and times.

This is my code:

#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;
}

But when I launch my app I get this error:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<PicturesViewController 0x758bf70> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key instaImage.'

My JSON-file looks like this:

[
    {
        "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"
    }
]

What am I doing wrong?

3 Answers 3

2

I think you have to check the property instaImage. I think you are accessing the property that you have not defined in nib.

Sign up to request clarification or add additional context in comments.

1 Comment

If cell is not a cell then other two property of the cell should also throw exception.
1

Fist try to debug this . From the crash log I think you are doing wrong with the custom cell . first check your connection of the PictureJSON . and check your file owner it should be your custom cell. You can confirm this using breakpoint . if you get error while cell creation then above is the solution .

1 Comment

why down vote ? Atleast take some effort to specify the reason .
0

I think the problem is not with your JSON serialization, from your log this crash because of your static NSString *simpleTableIdentifier = @"PictureJSON";

Make sure, you have assigned your custom cell with this identifier !

2 Comments

This is not a problem, you can use any string for identifier.
he is using [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; , if there isn t any cell with this identifier, it will crash

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.