-2

array.countをチェックするif/elseステートメントを避けようとしています。最小限のコードでこれを達成するための最良の方法は何ですか:

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

セクション数とセクションヘッダー.カウントは異なります: 最小 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サイズに基づいて変数を設定します。sectionsnil

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 に答える