我有一个NSMutableArray,并希望每次都添加一个带有整数参数的二维NSArray。但是在使用之后,它们的ID值不显示为Integers.And我也尝试了@[i,j]
但它没有用。这是我的代码:
resault = [[NSMutableArray alloc]initWithCapacity:40];
for (int i=0; i<5; i++)
{
for (int j=0; j<5; j++)
{
[resault addObject:@[@(i),@(j)]];
}
}
答案 0 :(得分:1)
使用此代码 -
NSMutableArray*resault = [[NSMutableArray alloc]initWithCapacity:40];
for (int i=0; i<5; i++)
{
NSMutableArray*array = [[NSMutableArray alloc]initWithCapacity:40];
for (int j=0; j<5; j++)
{
[array addObject:@[@(i),@(j)]];
}
[resault addObject:array];
}
答案 1 :(得分:1)
你可以试试这个:
NSMutableArray * ma = [NSMutableArray new];
for (int i = 0; i < 10; i++){
for (int d = 0; d < 10; d++){
[ma addObject:[NSString stringWithFormat:@"%i%i",i,d]];
}
}
NSLog(@"id is %@", ma);
循环遍历I循环,然后循环D循环并作为NSString对象添加到数组中。如果您需要它们作为NSNumbers,请稍后再转换它们。如果从0到n计数,则可以使用单个for循环(例如,在上面的示例中,0到100将起作用),而如果您使用非数字ID,嵌套for循环可以更好地工作。
如果要将ID保持在10以下的0前面,请使用单个for循环:
NSMutableArray * ma = [NSMutableArray new];
for (int i = 0; i < 100; i++){
[ma addObject:[NSString stringWithFormat:@"%02d",i]];
}
NSLog(@"id is %@", ma);
使用您的方法:
NSMutableArray * ma = [NSMutableArray new];
for (int i = 0; i < 10; i++){
for (int d = 0; d < 10; d++){
[ma addObject:@[@(i),@(d)]];
}
}
int thirtiethI = [ma[30][0] intValue];
int thirtiethD = [ma[30][1] intValue];
NSLog(@"thirtieth: %i %i", thirtiethI, thirtiethD);
答案 2 :(得分:1)
resault = [[NSMutableArray alloc] initWithCapacity:40];
for (int i=0; i<5; i++)
{
for (int j=0; j<5; j++)
{
[resault addObject:[NSArray arrayWithObjects:
[NSNumber numberwithInt:i],
[NSNumber numberWithInt:j],
nil]
];
}
}
然后,您可以通过运行结果数组并提取存储的NSArray并获取第一个和第二个对象来访问i,j对。
答案 3 :(得分:1)
首先你要了解int,float这些基本类型不能存入数组
所以当你添加@(i)时,这意味着[NSNumber numberWithInt:i]
数组是NSNumber的类型
如果你想把它拿出来
int result = [resault [30] [0] intValue];