我有一组Videos
对象,其中包括属性id
和tags
。建立一对多关系的最佳方式是什么?
我想要建立一个字典,其key
是tag
和其value
是id
的数组。
例如,一些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];
}
}
}
这代码看上去完全合理的我。你觉得有什么需要改进的地方吗?重构它会容易一点点,但它不会明显改变性能。 – 2013-04-11 15:36:47
不是特别的,比任何事情都更好奇。 – 2013-04-11 15:44:33