NSInvalidArgumentException reason: data parameter is nil in UITableView while trying to display the Flickr images

拈花ヽ惹草 提交于 2020-01-05 02:53:50

问题


Hi in my application I want to display the Flickr album list in UITableView so i have searched for long time and i have found some solution. I have used the method which given in the solution its not working its giving error like

NSInvalidArgumentException', reason: 'data parameter is nil

The solution link click here

And since I'm trying this for first time I'm not able resolve this issue. This is MY API LINK for Flickr

I have used this code to display the Flickr image Album list in UItableview

{
   NSMutableArray *photoURLs;
   NSMutableArray *photoSetNames;
   NSMutableArray *photoid1;
}

My Flickr API key

#define  FlickrAPIKey @"a6a0c7d5efccffc285b0fe5ee1d938e3"

  - (void)viewDidLoad
{
    [super viewDidLoad];
     photoURLs = [[NSMutableArray alloc] init];
     photoSetNames = [[NSMutableArray alloc] init];
     photoid1 = [[NSMutableArray alloc] init];
    [self loadFlickrPhotos];

}

My TableView code

  - (void)loadFlickrPhotos
 {
      NSString *urlString = [NSString stringWithFormat:@"http://api.flickr.com/services/rest/?method=flickr.photosets.getList&api_key=%@&user_id=%@&per_page=10&format=json&nojsoncallback=1", FlickrAPIKey, @"124757153@N04"];
      NSURL *url = [NSURL URLWithString:urlString];

      NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];

      NSDictionary *results = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
      NSArray *photosets = [[results objectForKey:@"photosets"] objectForKey:@"photoset"];
   for (NSDictionary *photoset in photosets) {

       NSString *title = [[photoset objectForKey:@"title"] objectForKey:@"_content"];
      [photoSetNames addObject:(title.length > 0 ? title : @"Untitled")];
       NSString *photoid = [photoset objectForKey:@"id"];
      [photoid1 addObject:photoid];

     }
  }
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

      return [photoSetNames count];
}

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *cellIdentifier =@"Cell";

     flickrpoliticalCell *cell =(flickrpoliticalCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  if (cell == nil) {

       cell = [[flickrpoliticalCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
     }
    cell.tit.text = [photoSetNames objectAtIndex:indexPath.row];
    return cell;
}

回答1:


try this

- (void)loadFlickrPhotos
{

//
NSString *urlString = [NSString stringWithFormat:@"https://www.flickr.com/services/rest/?method=flickr.photosets.getList&api_key=a6a0c7d5efccffc285b0fe5ee1d938e3&format=json&user_id=124757153@N04&per_page=10&nojsoncallback=1",nil];

NSLog(@"the url string==%@",urlString);
NSURL *url = [NSURL URLWithString:urlString];

NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];

NSLog(@"the str==%@",jsonString);

NSDictionary *results = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
   NSArray *photosets = [[results objectForKey:@"photosets"] objectForKey:@"photoset"];


for (NSDictionary *photoset in photosets) {

    NSString *title = [[photoset objectForKey:@"title"] objectForKey:@"_content"];
    NSLog(@"title==%@",title);
    [photoSetNames addObject:(title.length > 0 ? title : @"Untitled")];

     NSString *primary = [photoset objectForKey:@"primary"];

    NSString *server = [photoset objectForKey:@"server"];

     NSString *secret = [photoset objectForKey:@"secret"];

    NSString *farm = [photoset objectForKey:@"farm"];

    NSString *urlstr=[NSString stringWithFormat:@"http://farm%@.staticflickr.com/%@/%@_%@.jpg",farm,server,primary,secret];

     NSLog(@"your photo id==%@",urlstr);

    [photoids addObject:urlstr];





}
}


来源:https://stackoverflow.com/questions/23775625/nsinvalidargumentexception-reason-data-parameter-is-nil-in-uitableview-while-tr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!