我试图避免使用 if/else 语句来检查 array.count。用最少的代码完成此任务的最佳方法是什么:
NSMutableDictionary *_sectionMap;
NSMutableArray *sections;
NSMutableArray *sectionHeaderLabel;
section.count 和 sectionHeader.count 会有所不同:最少 1 个对象;最多 7 个对象;
index 3 beyond bounds [0 .. 2]'
目前,如果我的数组没有全部 7 个对象,我会收到错误消息。
[_sectionMap setValue:@"_iceCreamSectionObjects" forKey:kCoffee];
[_sectionMap setValue:@"_lunchSectionObjects" forKey:kLunch];
[_sectionMap setValue:@"_dinnerSectionObjects" forKey:kDinner];
[_sectionMap setValue:@"_movieSectionObjects" forKey:kMovies];
[_sectionMap setValue:@"_activity1SectionObjects" forKey:kActivity1];
[_sectionMap setValue:@"_activity2SectionObjects" forKey:kActivity2];
[_sectionMap setValue:@"_activity3SectionObjects" forKey:kActivity3];
NSArray *sectionZero = _recipientSectionObjects;
NSArray *sectionOne = _meetUpSectionObjects;
NSArray *sectionTwo = [_sectionMap valueForKey:[sections objectAtIndex:0]];
NSArray *sectionThree = [_sectionMap valueForKey:[sections objectAtIndex:1]];
NSArray *sectionFour = [_sectionMap valueForKey:[sections objectAtIndex:2]];
NSArray *sectionFive = [_sectionMap valueForKey:[sections objectAtIndex:3]];
NSArray *sectionSix = [_sectionMap valueForKey:[sections objectAtIndex:4]];
NSArray *sectionSeven = [_sectionMap valueForKey:[sections objectAtIndex:5]];
NSArray *sectionEight = [_sectionMap valueForKey:[sections objectAtIndex:6]];
NSString *sectionZeroLabel = @"Recipient";
NSString *sectionOneLabel = @"Meetup";
NSString *sectionTwoLabel = [sectionHeaderLabel objectAtIndex:0];
NSString *sectionThreeLabel = [sectionHeaderLabel objectAtIndex:1];
NSString *sectionFourLabel = [sectionHeaderLabel objectAtIndex:2];
NSString *sectionFiveLabel = [sectionHeaderLabel objectAtIndex:3];
NSString *sectionSixLabel = [sectionHeaderLabel objectAtIndex:4];
NSString *sectionSevenLabel = [sectionHeaderLabel objectAtIndex:5];
NSString *sectionEightLabel = [sectionHeaderLabel objectAtIndex:6];
NSString *sectionNineLabel = @"Transportation There";
NSString *sectionTenLabel = @"Transportation Back";