English 中文(简体)
物体空阵 页: 1 页: 1
原标题:empty array at objectForKey crashes iPhone app

I have an array that gets created from JSON. The array looks like this:

[
    {
        "img": "images/photo_10.jpg", 
        "title": "None", 
        "photo_comments": [
            {
                "body": "my comment", 
                "author": "john", 
                "created": "2011-04-17 14:21:11"
            }
        ], 
        "id": 24
    }, 
    {
        "img": "images/photo_8.jpg", 
        "title": "None", 
        "photo_comments": [], 
        "id": 22
    }


]

我通过这个阵列,履行一个职责,列举字典,并形成一个插图,然后增加评论阵列。 我的法典认为:

    -(NSArray *)formatCommentArray:(NSArray *)array  
{

    NSMutableArray *comments = [[[NSMutableArray alloc] init] autorelease];

    for (NSDictionary *photo in array)
    {   


           for( NSDictionary *comment in [photo objectForKey:@"photo_comments"])
           {

               NSString *commentString = [NSString stringWithFormat:@"%@: %@", 
                                           [comment objectForKey:@"author"], [comment objectForKey:@"body"]];



               [comments addObject:commentString];


           }




    return comments;

}

照片似乎坠毁,因为并非所有照片都有评论,而且当它进入空洞时,它就会停止。 我尝试了一些“如果说的话”和其他几个骗子,没有结果。 我成功地利用了该守则制造了一系列图像,但显然,“img”钥匙的价值在于NOT是一个有字典的阵列。 任何帮助都将受到高度赞赏。 提前感谢。

最佳回答

well, [[comment objectForKey:@"photo_comments"] objectForKey:@"author"] is trying to use objectForKey on an array, which won t work. Looking at your json, photo_comments is a (potentially empty) list, containing objects. You need one more list loop in there.

for (NSDictionary *photo in array)
{
  // do stuff with photo.img, photo.title etc

  for (NSDictionary *comment in [photo objectForKey:@"photo_comments"])
  {
    // do stuff with comment.author, comment.body etc
  }
}

(另类错误核对。)

问题回答

暂无回答




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签