Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


Downloading JSON image Problem

Hello guys, i made an exercise for the Master Detail Application regarding how to downloading the data using JSON and display it in table view. I can set the title and author using JSON but i have a little problem with images ( thumbnail ). The code doesn't give me any error but when i ran the application they give me an error " [__NSCFString isFileURL]: unrecognized selector sent to instance 0x8c9d5c0 " . Here is my code for MasterViewController.h

#import <UIKit/UIKit.h>

@interface MasterViewController : UITableViewController

@property (strong, nonatomic) NSArray *tajuk;


and here is my code for MasterViewController.m

#import "MasterViewController.h"

#import "DetailViewController.h"

@interface MasterViewController () {
    NSMutableArray *_objects;

@implementation MasterViewController

- (void)awakeFromNib
    [super awakeFromNib];

- (void)viewDidLoad
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //self.tajuk = [NSArray arrayWithObjects:@"barca",@"kelate",@"jdt", nil];

    NSURL *url = [NSURL URLWithString:@"http://blog.teamtreehouse.com/api/get_recent_summary/"];
    NSData *jsonData = [NSData dataWithContentsOfURL:url];
    NSError *error = nil;
    NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];

    self.tajuk = [dataDictionary objectForKey:@"posts"];

//    NSLog(@"%@", dataDictionary);

    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
    self.navigationItem.rightBarButtonItem = addButton;

- (void)didReceiveMemoryWarning
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

- (void)insertNewObject:(id)sender
    if (!_objects) {
        _objects = [[NSMutableArray alloc] init];
    [_objects insertObject:[NSDate date] atIndex:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    return 1;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    return self.tajuk.count;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    NSDictionary *dataFromWeb = [self.tajuk objectAtIndex:indexPath.row];

   // self.URL = [NSURL URLWithString:[dataFromWeb objectForKey:@"thumbnail"]];

//    NSString *object = self.tajuk[indexPath.row];
    cell.textLabel.text = [dataFromWeb objectForKey:@"title"];

   // NSLog(@"%@", [dataFromWeb objectForKey:@"thumbnail"]);

    if([[dataFromWeb objectForKey:@"thumbnail"] isKindOfClass:[NSString class]]) {

        NSData *imageData = [NSData dataWithContentsOfURL:[dataFromWeb objectForKey:@"thumbnail"]];
        UIImage *image = [UIImage imageWithData:imageData];
        cell.imageView.image = image;
        NSLog(@"%@", [dataFromWeb objectForKey:@"thumbnail"]);

    } else {

        cell.imageView.image = nil;


    return cell;

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSDictionary *dataWeb = [self.tajuk objectAtIndex:indexPath.row ];
        NSString *objek = [dataWeb objectForKey:@"author"];
        //NSString *object = self.tajuk[indexPath.row];
        [segue.destinationViewController setDetailItem:objek];


I got a breakpoint error at this line

NSData *imageData = [NSData dataWithContentsOfURL:[dataFromWeb objectForKey:@"thumbnail"]];

3 Answers

Thomas Nilsen
Thomas Nilsen
14,957 Points

[NSData dataWithContentsOfURL:] takes an NSURL. You need to put [dataFromWeb objectForKey:@"thumbnail"] (which is a string), into an NSURL first.

It would look something like this:

 NSURL *url = [NSURL URLWithString:[dataFromWeb objectForKey:@"thumbnail"]];
 NSData *imageData = [NSData dataWithContentsOfURL:url];

Thanks Thomas, now i solved my problem.

Thanks Thomas, now i solved my problem.