![](/img/trans.png)
[英]How to add an NSMutableArray to an NSMutableArray Objective-c
[英]How to add integer NSArrays objects with 2 dimention to NSMutableArray in Objective-C?
我有一个NSMutableArray,想每次向其添加带有整数参数的二维NSArray。 但是使用该值后,其ID不会显示为Integers。我也尝试了@[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)]];
}
}
使用此代码-
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];
}
您可以尝试以下方法:
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前面的0字符保持在10以下,请使用一个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);
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对。
首先您必须了解int,float这些基本类型不能被存入数组
因此,当您将其添加到@(i)时,这意味着[NSNumber numberWithInt:i]
数组是NSNumber的类型
如果你想拿出来
int result = [resault [30] [0] intValue];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.