开发者

How to dissect and reorganize info in an NSDictionary

开发者 https://www.devze.com 2023-03-03 20:03 出处:网络
So I have an array of NSDictionaries, each NSDictionary has a bunch of key/value pairs pertaining to aspects of a photo (from Flickr).

So I have an array of NSDictionaries, each NSDictionary has a bunch of key/value pairs pertaining to aspects of a photo (from Flickr).

I'm making an app that has a UITableViewController whose cells should be each of the different categories of the photos. So in pseudocode, I'm trying to construct a new NSDictionary (with keys being categories of photos, values being the NSDictionaries of the photos that contains that key). I'm iterating through each NSDictionary in the initial array, getting the category tags, and saying, if my new NSDict doesn't contain this key, make a new key to an empty array. Then add the current NSDict to that array. I'm getting consistent errors, not sure why.

Here's the diluted code.

  photoList = [FlickrFetcher photosWithTags:[NSArray arrayWithObjects: @"CS193p_SPoT", nil]];
    NSLog(@"%@", photoList);
    categories = [[NSDictionary alloc] init]; 
    NSArray *temp = [[NSArray alloc] init];
    for (id obj in photoList) {
        temp = [[obj objectForKey:@"tags"] componentsSeparatedByString:@" "];
        for (id string in temp) {
            if (![categories objectForKey:string]) {
                NSMutab开发者_开发知识库leArray *arr = [[NSMutableArray alloc] init];
               [categories setObject:arr forKey:string];
                //[arr release];
            }
            NSMutableArray *photos = [categories objectForKey:string];
            [photos addObject:obj];
            [categories setObject:photos forKey:string];
        }
    }

Thanks!


NSDictionary doesn't have a method setObject:forKey:. You need an NSMutableDictionary.

self.categories = [NSMutableDictionary dictionary];

Other than that, please do use Joost's excellent rewrite of your code.

SIGABRT, just so you know, most likely means that an assertion somewhere failed. In this case, it may be an assertion all the way down in CoreFoundation*; CF checks for mutability when you try to access a dictionary like that and causes an interrupt if the object isn't mutable.


*I have just learned about the CF source's availability recently and have been looking through it, so this may be just "new thing" bias and incorrect.


I don't notice any errors (syntax-errors, that is) in your code, however here is an updated piece of code which has been implemented a bit cleaner (and without memory leaks)

self.photoList = [FlickrFetcher photosWithTags:[NSArray arrayWithObjects: @"CS193p_SPoT", nil]];
NSLog(@"%@", photoList);
self.categories = [NSDictionary dictionary];
for (NSDictionary *obj in photoList) {
    NSArray *temp = [[obj objectForKey:@"tags"] componentsSeparatedByString:@" "];
    for (NSString *string in temp) {
        NSMutableArray *photos = [categories objectForKey:string];
        if (!photos) {
           photos = [NSMutableArray array];
           [categories setObject:photos forKey:string];
        }
        [photos addObject:obj];
    }
}

If it's not working please tell us the exact warning, and were it is caused.

0

精彩评论

暂无评论...
验证码 换一张
取 消