-2

我试图避免使用 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";
4

1 回答 1

2

我在猜测,但我认为您正在寻找一个计算的 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。

于 2013-09-05T00:37:58.457 回答