NSDictionary用字典作为对象,获取对象

时间:2012-02-09 10:32:14

标签: iphone ios nsarray nsdictionary

我有一个像这样的NSDictionary。

PhotoData =     {
    1 =         {
        "jf_photo_geotag" = "arayat philippines";
        "jf_photo_id" = 1;
        "jf_photo_url" = "http://adomainname/iphone_images/large/jf_largeimage1.png";
        "jf_user_id" = 1;
        comments =             {
            1 =                 {
                "jf_photo_comment" = "well i like this one. really. comment from janus";
                "jf_photo_comment_id" = 1;
                "jf_photo_id" = 1;
                "jf_user_id" = 1;
                "jf_username" = janus;
            };
            4 =                 {
                "jf_photo_comment" = asasfa;
                "jf_photo_comment_id" = 4;
                "jf_photo_id" = 1;
                "jf_user_id" = 1;
                "jf_username" = janus;
            };
        };
        likes =             {
            2 =                 {
                "jf_like_id" = 2;
                "jf_photo_id" = 1;
                "jf_user_id" = 1;
                "jf_username" = janus;
            };
        };
        "post_date" = "2012-02-23 16:34:14";
    };
    2 =         {
        "jf_photo_geotag" = "magalang philippines";
        "jf_photo_id" = 2;
        "jf_photo_url" = "http://adomainname/iphone_images/large/jf_largeimage2.png";
        "jf_user_id" = 2;
        comments =             {
            2 =                 {
                "jf_photo_comment" = "well this is now a comment from loaclhost! for phot number 2";
                "jf_photo_comment_id" = 2;
                "jf_photo_id" = 2;
                "jf_user_id" = 2;
                "jf_username" = fidel;
            };
            3 =                 {
                "jf_photo_comment" = asdfadf;
                "jf_photo_comment_id" = 3;
                "jf_photo_id" = 2;
                "jf_user_id" = 2;
                "jf_username" = fidel;
            };
        };
        likes =             {
            1 =                 {
                "jf_like_id" = 1;
                "jf_photo_id" = 2;
                "jf_user_id" = 1;
                "jf_username" = janus;
            };
        };
        "post_date" = "2012-02-23 16:34:31";
    };
    3 =         {
        "jf_photo_geotag" = "san fdo philippines";
        "jf_photo_id" = 3;
        "jf_photo_url" = "http://adomainname/iphone_images/large/jf_largeimage1.png";
        "jf_user_id" = 3;
        comments =             (
        );
        likes =             (
        );
        "post_date" = "2012-02-24 16:34:47";
    };
  };
success = 1;

}

基本上,这个NSDictionary包含一些字符串数据,如jf_photo_url,jf_photo_id等,还有另一个字典作为其对象,如comments =()

我如何使用这个词典来改变它的结构和产生,并可能创建一个类似于这个的新词典:

PhotoData =     {
    {
        "jf_photo_geotag" = "arayat philippines";
        "jf_photo_id" = 1;
        "jf_photo_url" = "http://adomainname/iphone_images/large/jf_largeimage1.png";
        "jf_user_id" = 1;
        comments = (
            {
                "jf_photo_comment" = "well i like this one. really. comment from janus";
                "jf_photo_comment_id" = 1;
                "jf_photo_id" = 1;
                "jf_user_id" = 1;
                "jf_username" = janus;
            },
            {
                "jf_photo_comment" = asasfa;
                "jf_photo_comment_id" = 4;
                "jf_photo_id" = 1;
                "jf_user_id" = 1;
                "jf_username" = janus;
            }
        };
        likes =  (
                {
                "jf_like_id" = 2;
                "jf_photo_id" = 1;
                "jf_user_id" = 1;
                "jf_username" = janus;
            }
         ,"post_date" = "2012-02-23 16:34:14";
    },

    {
        "jf_photo_geotag" = "magalang philippines";
        "jf_photo_id" = 2;
        "jf_photo_url" = "http://adomainname/iphone_images/large/jf_largeimage2.png";
        "jf_user_id" = 2;
        comments =(
                {
                "jf_photo_comment" = "well this is now a comment from loaclhost! for phot number 2";
                "jf_photo_comment_id" = 2;
                "jf_photo_id" = 2;
                "jf_user_id" = 2;
                "jf_username" = fidel;
            },
            {
                "jf_photo_comment" = asdfadf;
                "jf_photo_comment_id" = 3;
                "jf_photo_id" = 2;
                "jf_user_id" = 2;
                "jf_username" = fidel;
            };
        };
        likes = (
            {
                "jf_like_id" = 1;
                "jf_photo_id" = 2;
                "jf_user_id" = 1;
                "jf_username" = janus;
            };
        },
        "post_date" = "2012-02-23 16:34:31";
    },
     {
        "jf_photo_geotag" = "san fdo philippines";
        "jf_photo_id" = 3;
        "jf_photo_url" = "http://adomainname/iphone_images/large/jf_largeimage1.png";
        "jf_user_id" = 3;
        comments =             (
        );
        likes =             (
        );
        "post_date" = "2012-02-24 16:34:47";
    };
  };
success = 1;

}

注意第一个字典在每个对象中都有索引号。我想删除它们。 我的第一个字典是从服务器加载的,第二个字典来自plist

谢谢你们!我希望你理解我的问题。

1 个答案:

答案 0 :(得分:2)

看起来您希望外部字典成为数组。假设您已经在NSDictionary中拥有此数据,您可以将其复制到如下数组:

NSDictionary* photoDict = some_dict; //your outer dict
NSMutableArray* photoArray = [[[NSMutableArray alloc] init] autorelease];

NSEnumerator *enumerator = [photoDict objectEnumerator];
id value;
while((value = [enumerator nextObject])) {
  [photoArray addObject:value];
}

//do something with photoArray