2015-11-05 48 views
0

我在iOS开发是新的,我想写下以下的最佳方式的场景:如何优化嵌套NSMutableArray代码的最佳方式?

- (void)configureDataSource { 
    NSMutableArray *meterUnitArray = [[NSMutableArray alloc] init]; 
    NSMutableArray *meterSubUnitArray = [[NSMutableArray alloc] init]; 
    NSMutableArray *footUnitArray = [[NSMutableArray alloc] init]; 
    NSMutableArray *footSubUnitArray = [[NSMutableArray alloc] init]; 
    self.footArray = [[NSMutableArray alloc] init]; 
    self.meterArray = [[NSMutableArray alloc] init]; 


    for(int i = 0; i <= 99; i++) 
    { 
     NSString *str = [NSString stringWithFormat:@"%d Meter", i]; 
     NSString *str1 = [NSString stringWithFormat:@"%d cm", i]; 
     [meterUnitArray addObject:str]; 
     [meterSubUnitArray addObject:str1]; 


     NSString *str2 = [NSString stringWithFormat:@"%d Foot", i]; 
     NSString *str3 = [NSString stringWithFormat:@"%d Inches", i]; 
     [footUnitArray addObject:str2]; 
     [footSubUnitArray addObject:str3]; 
    } 

    [self.meterArray addObject:meterUnitArray]; 
    [self.meterArray addObject:meterSubUnitArray]; 

    [self.footArray addObject:footUnitArray]; 
    [self.footArray addObject:footSubUnitArray]; 
} 

我怎么能改进这个代码?

+0

你想实现什么? – ajpallares

+0

我认为这个问题更属于这里:http://programmers.stackexchange.com/ – Losiowaty

+0

或甚至更好,在这里:http://codereview.stackexchange.com/ – Losiowaty

回答

1
@interface YourClass() 

@property (strong, nonatomic) NSArray *footArray; 
@property (strong, nonatomic) NSArray *meterArray; 

@end 

@implementation YourClass 

- (void)configureDataSource { 
    self.meterArray = @[[@[] mutableCopy], [@[] mutableCopy]]; 
    self.footArray = @[[@[] mutableCopy], [@[] mutableCopy]]; 

    for(int i = 0; i <= 99; i++) { 
    [self.meterArray[0] addObject:[NSString stringWithFormat:@"%d Meter", i]]; 
    [self.meterArray[1] addObject:[NSString stringWithFormat:@"%d cm", i]]; 

    [self.footArray[0] addObject:[NSString stringWithFormat:@"%d Foot", i]]; 
    [self.footArray[1] addObject:[NSString stringWithFormat:@"%d Inches", i]]; 
    } 
}