如何设置数组索引值
How To Set The Array Index Value
我想设置数组索引值从1开始,因为我显示的是数组索引值我不想把索引值设置为零。我不知道如何从零设置数组索引值。
Array (
[0] => "please"
[1] => "save"
[2] => "water"
)
我想要这样:
Array (
[1] => "please"
[2] => "save"
[3] => "water"
)
我在标签中显示集合视图中每个对象的索引值。
请提出一些想法和示例代码来执行此操作。
这是我的解决方案
NSMutableArray *array1 = [NSMutableArray arrayWithObjects: @"please", @"save",@"water", nil];
[array1 replaceObjectAtIndex:0 withObject:@" "];
[array1 replaceObjectAtIndex:1 withObject:@"please"];
[array1 replaceObjectAtIndex:2 withObject:@"save"];
[array1 insertObject:@"water" atIndex:3];
NSLog(@"The result is - %@",array1);
打印结果为
The result is - (
" ",
please,
save,
water
)
为了直观呈现,只需在显示之前将索引加 1。
int indexInArray = /* whatever */;
NSString *displayedString = [NSString stringWithFormat:@"%i", indexInArray + 1];
yourLabel.text = displayedString;
我想设置数组索引值从1开始,因为我显示的是数组索引值我不想把索引值设置为零。我不知道如何从零设置数组索引值。
Array (
[0] => "please"
[1] => "save"
[2] => "water"
)
我想要这样:
Array (
[1] => "please"
[2] => "save"
[3] => "water"
)
我在标签中显示集合视图中每个对象的索引值。 请提出一些想法和示例代码来执行此操作。
这是我的解决方案
NSMutableArray *array1 = [NSMutableArray arrayWithObjects: @"please", @"save",@"water", nil];
[array1 replaceObjectAtIndex:0 withObject:@" "];
[array1 replaceObjectAtIndex:1 withObject:@"please"];
[array1 replaceObjectAtIndex:2 withObject:@"save"];
[array1 insertObject:@"water" atIndex:3];
NSLog(@"The result is - %@",array1);
打印结果为
The result is - (
" ",
please,
save,
water
)
为了直观呈现,只需在显示之前将索引加 1。
int indexInArray = /* whatever */;
NSString *displayedString = [NSString stringWithFormat:@"%i", indexInArray + 1];
yourLabel.text = displayedString;