Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Brett Kim
11,576 PointsOops! BlogReader Image is not displaying
I don't know why the code isn't working. I noticed that the code wasn't working and it wasn't crashing AND it wasn't displaying an image. So I don't know where the error is because I'm not getting an error anywhere. I added code in to make sure that if an NSNull showed up, it would be replaced by the treehouse logo. Now all of my icons have Treehouse logos on them. Do you know why I'm receiving so many NSNulls?
Here's my code:
''' /BlogPost.h/
import <Foundation/Foundation.h>
@interface BlogPost : NSObject
@property (nonatomic, strong) NSString *title; @property (nonatomic, strong) NSString *author; @property (nonatomic, strong) NSString *thumbnail;
//Designated Initializer
- (id) initWithTitle:(NSString *)title;
(id) blogPostWithTitle:(NSString *)title;
(NSURL *) thumbnailURL;
@end
/BlogPost.m/
import "BlogPost.h"
@implementation BlogPost
-
(id) initWithTitle:(NSString *)title { self = [super init];
if( self ) { self.title = title; self.author = nil; self.thumbnail = nil; } return self; }
+(id) blogPostWithTitle:(NSString *)title { return [[self alloc] initWithTitle:title]; }
- (NSURL *) thumbnailURL { return [NSURL URLWithString:self.thumbnail]; }
@end
/TableViewController.h/
import <UIKit/UIKit.h>
@interface TableViewController : UITableViewController
@property (nonatomic, strong) NSMutableArray *blogPosts;
@end
/TableViewController.m/ /Only Relevant Code Involving The Thumbnail/ /*OK, fine, mostly relevant. :) */
-
(void)viewDidLoad { [super viewDidLoad];
NSURL *blogURL = [NSURL URLWithString:@"http://blog.teamtreehouse.com/api/get_recent_summary/"]; NSData *jsondata = [NSData dataWithContentsOfURL:blogURL]; NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsondata options:0 error:&error];
self.blogPosts = [NSMutableArray array]; NSArray *blogPostsArray = [dataDictionary objectForKey:@"posts"]; for (NSDictionary *bpDictionary in blogPostsArray) { BlogPost *blogPost = [BlogPost blogPostWithTitle:[bpDictionary objectForKey:@"title"]]; blogPost.author = [bpDictionary objectForKey:@"author"]; blogPost.thumbnail = [bpDictionary objectForKey:@"thumbnnail"]; [self.blogPosts addObject:blogPost];
} }
-
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
BlogPost *blogPost = [self.blogPosts objectAtIndex:indexPath.row];
if ( [blogPost.thumbnail isKindOfClass:[NSString class]]) { NSData *imageData = [NSData dataWithContentsOfURL:blogPost.thumbnailURL]; UIImage *image = [UIImage imageWithData:imageData]; cell.imageView.image = image; } else { cell.imageView.image = [UIImage imageNamed:@"treehouse.png"]; }
cell.textLabel.text = blogPost.title; cell.detailTextLabel.text = blogPost.author;
return cell; '''
Anyways, it would be great if someone could help me out! Thanks Amit Bijlani for making this awesome track. I'm really enjoying learning more about iOS!
2 Answers
Amit Bijlani
Treehouse Guest TeacherYou misspelled thumbnail in your viewDidLoad method when assigning it to blogPost.thumbnail. The best thing to do when you have situations like these is to put in NSLogs to inspect the value of variables and properties.
blogPost.thumbnail = [bpDictionary objectForKey:@"thumbnail"];
Eliezer Marte
4,728 PointsI too only get an NSLog
import "TableViewController.h"
import "BlogPost.h"
@interface TableViewController ()
@end
@implementation TableViewController
(id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if (self) { // Custom initialization } return self; }
-
(void)viewDidLoad { [super viewDidLoad];
NSURL *blogURL = [NSURL URLWithString:@"http://blog.teamtreehouse.com/api/get_recent_summary/"];
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSLog(@"%@", dataDictionary);
self.blogPosts = [NSMutableArray array];
NSArray *blogPostsArray = [dataDictionary objectForKey:@"post"];
for (NSDictionary *bpDictionary in blogPostsArray) { BlogPost *blogPost = [BlogPost blogPostWithTitle:[bpDictionary objectForKey:@"title"]]; blogPost.author = [bpDictionary objectForKey:@"author"]; blogPost.thumbnail = [bpDictionary objectForKey:@"thumbnail"]; [self.blogPosts addObject:blogPost];
}
}
- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
pragma mark - Table view data source
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; }
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return [self.blogPosts count]; }
-
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
BlogPost *blogPost = [self.blogPosts objectAtIndex:indexPath.row];
NSData *imageData = [NSData dataWithContentsOfURL:blogPost.thumbnailURL]; UIImage *image = [UIImage imageWithData:imageData];
cell.imageView.image = image; cell.textLabel.text = blogPost.title; cell.detailTextLabel.text = blogPost.author;
return cell; }
Brett Kim
11,576 PointsBrett Kim
11,576 PointsThanks! I'll try using NSLogs for bug-testing more.