C/Objective-C:重构数组组件



我试图避免使用if/else语句来检查array.count。用最少的代码实现这一点的最佳方法是什么:

NSMutableDictionary *_sectionMap;
NSMutableArray *sections;
NSMutableArray *sectionHeaderLabel;

sections.count&sectionsHeader.count会有所不同:至少1个Object;最多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";

我猜,但我认为您正在寻找一个计算的goto,这正是switch语句的样子。假设您使用的是ARC,因此引用变量默认为nil,下面的代码将根据sections的大小设置变量,剩余的变量为nil:

NSArray *sectionZero, *sectionOne, *sectionTwo, *sectionThree, *sectionFour, *sectionFive, *sectionSix, *sectionSeven, *sectionEight;
switch (sections.count)
{
   case 7:
      sectionEight = [_sectionMap valueForKey:[sections objectAtIndex:6]];
   case 6:
      sectionSeven = [_sectionMap valueForKey:[sections objectAtIndex:5]];
   case 5:
      sectionSix = [_sectionMap valueForKey:[sections objectAtIndex:4]];
   case 4:
      sectionFive = [_sectionMap valueForKey:[sections objectAtIndex:3]];
   case 3:
      sectionFour = [_sectionMap valueForKey:[sections objectAtIndex:2]];
   case 2:
      sectionThree = [_sectionMap valueForKey:[sections objectAtIndex:1]];
   case 1:
      sectionTwo = [_sectionMap valueForKey:[sections objectAtIndex:0]];
      sectionOne = _meetUpSectionObjects;
      sectionZero = _recipientSectionObjects;
      break;
   default:
      // handle error
}

请注意缺少break语句——每个标签都会一直延伸到下一个。

HTH。