如何剖析和重组 NSDictionary 中的信息

发布于 2024-11-05 12:08:30 字数 1135 浏览 2 评论 0原文

所以我有一个 NSDictionaries 数组,每个 NSDictionary 都有一堆与照片的各个方面相关的键/值对(来自 Flickr)。

我正在制作一个具有 UITableViewController 的应用程序,其单元格应该是照片的每个不同类别。因此,在伪代码中,我尝试构造一个新的 NSDictionary (键是照片类别,值是包含该键的照片的 NSDictionaries)。我正在迭代初始数组中的每个 NSDictionary,获取类别标签,并说,如果我的新 NSDict 不包含此键,请为空数组创建一个新键。然后将当前的 NSDict 添加到该数组中。我不断收到错误,不知道为什么。

这是稀释后的代码。

  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]) {
                NSMutableArray *arr = [[NSMutableArray alloc] init];
               [categories setObject:arr forKey:string];
                //[arr release];
            }
            NSMutableArray *photos = [categories objectForKey:string];
            [photos addObject:obj];
            [categories setObject:photos forKey:string];
        }
    }

谢谢!

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]) {
                NSMutableArray *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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜心 2024-11-12 12:08:30

NSDictionary 没有方法 setObject:forKey:。您需要一个NSMutableDictionary

self.categories = [NSMutableDictionary dictionary];

除此之外,请务必使用 Joost 对代码的出色重写。

SIGABRT,正如您所知,很可能意味着某个地方的断言失败了。在这种情况下,它可能是 CoreFoundation* 中一直向下的断言;当您尝试访问这样的字典时,CF 会检查可变性,如果对象不可变,则会导致中断。


*我最近刚刚了解到CF 源的可用性,并且已经一直在研究它,所以这可能只是“新事物”偏见和不正确的。

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.

找回味觉 2024-11-12 12:08:30

我没有注意到您的代码中有任何错误(即语法错误),但是这里有一段更新的代码,它的实现更加干净(并且没有内存泄漏)

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];
    }
}

如果它不起作用,请告诉我们确切的警告,并且它是引起的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文