Reputation: 33
I'm working on a Facebook Feed App for the iPhone that, among other things, determines if a picture is attached to a post, and stores it in an array. When I run the code, my NSLogs tell me that nothing is being put into the array, even though it's reading whether or not there is an objectAtKey:@"picture". The following is some of my code.
from MasterView.m:
//create array containing individual "datas"
NSArray *items = [json objectForKey:@"data"];
for(NSDictionary *item in items)
{
// store message in ItemStore sharedStore
if([item objectForKey:@"message"] || [item objectForKey:@"message"] != nil ||
[[item objectForKey:@"message"] length] > 0){
[[JSONFeedItemStore sharedStore] createItem:[item objectForKey:@"message"]];
}
//
if([item objectForKey:@"picture"]){
[[JSONFeedItemStore sharedStore] createPicture:[[item objectForKey:@"picture"] description]];
NSLog(@"url: %@", [item objectForKey:@"picture"]);
} else {
[[JSONFeedItemStore sharedStore] createPicture:@"http://i.imgur.com/TpIK5.png"]; // blank
NSLog(@"creating blank picture");
}
}
from ItemStore.m
- (void)createPicture:(NSString *)pictureUrl
{
[pictures addObject:pictureUrl];
NSLog(@"Number: %d, URL: %@", [pictures count], [pictures objectAtIndex:[pictures count]]);
}
and my console
2012-08-07 08:21:54.153 JSONFeed[2502:f803] Number: 0, URL: (null)
2012-08-07 08:21:54.154 JSONFeed[2502:f803] creating blank picture
2012-08-07 08:21:54.155 JSONFeed[2502:f803] Number: 0, URL: (null)
2012-08-07 08:21:54.156 JSONFeed[2502:f803] creating blank picture
2012-08-07 08:21:54.157 JSONFeed[2502:f803] Number: 0, URL: (null)
2012-08-07 08:21:54.157 JSONFeed[2502:f803] url: http://photos-a.ak.fbcdn.net/hphotos-ak-ash4/423482_427478620624383_82270372_s.jpg
2012-08-07 08:21:54.158 JSONFeed[2502:f803] Number: 0, URL: (null)
2012-08-07 08:21:54.158 JSONFeed[2502:f803] creating blank picture
SharedStore is a part of an ItemStore class created to store the messages and pictures from Facebook posts. If you have any questions, or need to see more code, feel free to ask. I'm also taking any suggestions for improvements, as I am still new to programming Apps.
Upvotes: 0
Views: 107
Reputation: 17732
For one, when you do anything like this;
[array objectAtIndex:[array count]];
You are going to get a nil
object, because by definition objectAtIndex:array.count
is beyond the bounds of the array, as all arrays on programming are 0-indexed
What you need is
[array objectAtIndex([array count]-1)];
Upvotes: 3
Reputation: 124997
One possibility is that pictures
is nil. You can't add objects to an array if the array doesn't exist, and it's legal to send messages to nil. The result you get back will also be nil or zero, so your call to -objectAtIndex:
logs "(null)".
Upvotes: 3