我使用Parse作为我的后端服务,但是对于这个示例,我创建了两个示例数组 (songs
和ratings
) 来模仿我的后端架构。songs
包含将填充我的应用程序表的歌曲数据。ratings
包括当前用户对歌曲的评分。
最终,我需要遍历songs
并将ratings
其嵌入到userRating
相应的songs
字典中。我在下面包含了我的循环代码。这可以更有效地完成吗?如果对象太多,我担心会花费太长时间ratings
。
NSMutableArray *songs = [@[ @{
@"objectId" : @"111",
@"title" : @"Song Title" },
@{
@"objectId" : @"222",
@"title" : @"Song Title"
} ] mutableCopy];
NSMutableArray *ratings = [@[ @{
@"objectId" : @"999",
@"parentObjectId" : @"111",
@"userRating" : @4
} ] mutableCopy];
for (NSInteger a = 0; a < songs.count; a++) {
NSMutableDictionary *songInfo = [songs objectAtIndex:a];
NSString *songObjectId = [songInfo objectForKey:@"objectId"];
NSNumber *userRating = @0;
for (NSInteger i = 0; i < ratings.count; i++) {
NSDictionary *userRatingInfo = [ratings objectAtIndex:i];
NSString *parentObjectId = [userRatingInfo objectForKey:@"parentObjectId"];
if ([parentObjectId isEqualToString:songObjectId]) {
userRating = [userRatingInfo objectForKey:@"userRating"];
}
}
[songInfo setObject:userRating forKey:@"userRating"];
}