有没有办法缩短这段代码?
NSMutableArray *allobjects = [[NSMutableArray alloc] initWithCapacity:b];
if (b == 1) {
[allobjects addObject:object];
}else if (b == 2){
[allobjects addObject:object];
[allobjects addObject:object2];
}else if (b == 3){
[allobjects addObject:object];
[allobjects addObject:object2];
[allobjects addObject:object3];
}else if (b == 4){
[allobjects addObject:object];
[allobjects addObject:object2];
[allobjects addObject:object3];
[allobjects addObject:object4];
}else if (b == 5){
[allobjects addObject:object];
[allobjects addObject:object2];
[allobjects addObject:object3];
[allobjects addObject:object4];
[allobjects addObject:object5];
}else if (b == 6){
[allobjects addObject:object];
[allobjects addObject:object2];
[allobjects addObject:object3];
[allobjects addObject:object4];
[allobjects addObject:object5];
[allobjects addObject:object6];
}else if (b == 7){
[allobjects addObject:object];
[allobjects addObject:object2];
[allobjects addObject:object3];
[allobjects addObject:object4];
[allobjects addObject:object5];
[allobjects addObject:object6];
[allobjects addObject:object7];
}else if (b == 8){
[allobjects addObject:object];
[allobjects addObject:object2];
[allobjects addObject:object3];
[allobjects addObject:object4];
[allobjects addObject:object5];
[allobjects addObject:object6];
[allobjects addObject:object7];
[allobjects addObject:object8];
}
正如您所看到的,如果b
等于1,我会添加object1
,如果b
为2,我会同时添加object1
和object2
,所以上。
答案 0 :(得分:3)
将所有这些字符串放入C数组中:
NSString * objects[] = { object1, object2, object3, object4, object5, object6, object7, object8, object9 };
一旦你知道b
的值是什么,就迭代一遍:
allObjects = [NSMutableArray array];
for( int i = 0; i < b; i++ ){
[allObjects addObject:objects[i]];
}
或者,甚至更好,做:
allObjects = [NSMutableArray arrayWithObjects:objects count:b];
答案 1 :(得分:1)
NSString* ojbs[] = { object1, object2, object3, object4, object5, object6, object7, object8, object9 };
int _count = 5; // for example
NSArray* arr = [NSArray arrayWithObjects: objs count: _count];
NSMutableArray* result = [NSMutableArray arrayWithCapacity: _count];
[resul addObjectsFromArray: arr];
答案 2 :(得分:1)
NSArray *allStrings = [NSArray arrayWithObjects:object, object2, object3, object4, object5, object6, object7, object8, object9, nil];
NSArray *objects = nil;
if (b <= [allStrings count])
objects = [allStrings subarrayWithRange:NSMakeRange(0, b)];
答案 3 :(得分:1)
以下是两个备选答案:
1
如何动态执行此操作(假设您可以将object
重命名为object0
以适合该模式!)
for (uint n = 0; n < b; ++n) {
NSString *name = [NSString stringWithFormat:@"object%i], n];
[allObjects addObject:[self performSelector:name]];
}
但是,这在运行时比其他基于数组的答案慢(每次在viewDidLoad
中都可以创建一个新的字符串时)。
2
另一种方法是使用视图的tag
属性 - 然后你的循环就会变成:
for (uint n = 0; n < b; ++n)
[allObjects addObject:[self.view viewWithTag:n]];
答案 4 :(得分:1)
感叹!
switch (b) {
case 8 :
[allobjects insertObject:object8 atIndex:0];
case 7 :
[allobjects insertObject:object7 atIndex:0];
case 6 :
[allobjects insertObject:object6 atIndex:0];
case 5 :
[allobjects insertObject:object5 atIndex:0];
case 4 :
[allobjects insertObject:object4 atIndex:0];
case 3 :
[allobjects insertObject:object3 atIndex:0];
case 2 :
[allobjects insertObject:object2 atIndex:0];
case 1 :
[allobjects insertObject:object atIndex:0];
break;
default :
<Signal error or whatever>
}
如果条目不需要按顺序排列,则可以使用addObject
。
您还可以使用OP原始代码的变体:
if (b >= 1) [allobjects addObject:object];
if (b >= 2) [allobjects addObject:object2];
if (b >= 3) [allobjects addObject:object3];
if (b >= 4) [allobjects addObject:object4];
if (b >= 5) [allobjects addObject:object5];
if (b >= 6) [allobjects addObject:object6];
if (b >= 7) [allobjects addObject:object7];
if (b >= 8) [allobjects addObject:object8];