gpt4 book ai didi

ios - 如何以最佳方式优化 Nested NSMutableArray 代码?

转载 作者:行者123 更新时间:2023-11-29 01:30:09 24 4
gpt4 key购买 nike

我是 IOS 开发的新手,我想以最好的方式写下以下场景:

- (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];
}

我怎样才能优化这段代码?

最佳答案

@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]];
}
}

关于ios - 如何以最佳方式优化 Nested NSMutableArray 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33542711/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com