2013-04-11 75 views
0

我有一组Videos对象,其中包括属性idtags建立一对多关系的最佳方式是什么?

我想要建立一个字典,其keytag和其valueid的数组。

例如,一些Video对象可能是这样的:

Video{ id:1, tags:[funny,political,humor] }

Video{ id:2, tags:[political,america] }

我想要的结果字典是这样的:

VideosWithTags["funny":[1]; "political":[1,2]; "humor":[1]; "america":[2]]

有一个标准算法来完成这个?

目前我在做这样的事情:

for (NSDictionary *video in videos) 
{ 
    NSNumber *videoId = [video objectForKey:@"id"]; 
    NSArray *tags = [video objectForKey:@"tags"]; 

    for (NSString *tag in tags) 
    { 
     NSMutableArray *videoIdsForTag = nil; 

     if ([videosAndTags objectForKey:tag] != nil) //same tag with videoIds already exists 
     { 
      videoIdsForTag = [videosAndTags objectForKey:tag]; 
      [videoIdsForTag addObject:videoId]; 

      //add the updated array to the tag key 
      [videosAndTags setValue:videoIdsForTag forKey:tag]; 
     } 
     else //tag doesn't exist yet, create it and add the videoId to a new array 
     { 
      NSMutableArray *videoIds = [NSMutableArray array]; 
      [videoIds addObject:videoId]; 

      //add the new array to the tag key 
      [videosAndTags setObject:videoIds forKey:tag]; 
     } 
    } 
} 
+0

这代码看上去完全合理的我。你觉得有什么需要改进的地方吗?重构它会容易一点点,但它不会明显改变性能。 – 2013-04-11 15:36:47

+0

不是特别的,比任何事情都更好奇。 – 2013-04-11 15:44:33

回答

1

您可以通过使用新的文本语法这看起来有点清洁。

我认为你可以通过让if分支做更少的工作而受益。例如你会试图获取videoIds数组,那么如果它不存在更好 - 创建它,并把它添加到videosAndTags对象,然后在该点之后的代码可以不重逻辑

for (NSDictionary *video in videos) { 
    NSNumber *videoId = video[@"id"]; 
    NSArray *tags = video[@"tags"]; 

    for (NSString *tag in tags) { 
    NSMutableArray *videoIds = videosAndTags[tag]; 
    if (!videoIds) { 
     videoIds = [NSMutableArray array]; 
     videosAndTags[tag] = videoIds; 
    } 

    // This is the only line where I manipulate the array 
    [videoIds addObject:videoId]; 
    } 
} 
+0

不错。这是我正在寻找的。不知道这些文字! – 2013-04-11 15:45:40

1
NSArray* videos = 
    @[@{ @"id" : @1, @"tags" : @[ @"funny", @"political", @"humor" ] }, 
     @{ @"id" : @2, @"tags" : @[ @"political", @"america" ] } ]; 
NSMutableDictionary* videosAndTags = [NSMutableDictionary new]; 

// find distinct union of tags 
NSArray* tags = [videos valueForKeyPath: @"@distinctUnionOfArrays.tags"]; 

// for each unique tag 
for(NSString* tag in tags) 
{ 
    // filter array so we only have ones that have the right tag 
    NSPredicate* p = [NSPredicate predicateWithFormat: @"tags contains %@", tag]; 
    videosAndTags[ tag ] = [[videos filteredArrayUsingPredicate: p] valueForKeyPath: @"id"]; 
} 
一致

这是另一种使用NSPredicate和valueForKeyPath的方法。

我不使用他们经常,但有时他们可以证明是有用的。

(我认为他们称之为事物的函数式编程风格,但我不是很确定)

NSPredicate reference
Key Value Coding

相关问题