나는 Videos
의 배열을 가지고 있는데, 그 중에서도 특히 id
및 tags
속성이 있습니다.일대 다 관계를 구축하는 가장 좋은 방법은 무엇입니까?
나는 그 key
tag
이며, 그 value
id
년대의 배열입니다 사전을 구축하고자합니다.
예를 들어, 일부 Video
객체는 다음과 같습니다
VideosWithTags["funny":[1]; "political":[1,2]; "humor":[1]; "america":[2]]
이 있습니까 :
Video{ id:1, tags:[funny,political,humor] }
Video{ id:2, tags:[political,america] }
나는 결과 사전과 같이 할 달성하기위한 표준 알고리즘 이?
현재 나는이 같은 일을 해요 :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];
}
}
}
그 코드는 나에게 완벽하게 합리적인 보이는
는 (나는 그들이이에게 사물의 기능적 프로그래밍 스타일을 호출 생각하지만, 난 그렇게 확실하지 않다). 개선이 필요하다고 생각하는 부분이 있습니까? 조금 더 짧게 만드는 것은 리팩토링하기 쉽지만, 성능을 상당히 바꿀 수는 없습니다. –
특히, 무엇보다 호기심. –