简体   繁体   English

如何将2维的整数NSArrays对象添加到Objective-C中的NSMutableArray?

[英]How to add integer NSArrays objects with 2 dimention to NSMutableArray in Objective-C?

I have a NSMutableArray and want to add a 2 dimensional NSArray with integer parameters to it each time. 我有一个NSMutableArray,想每次向其添加带有整数参数的二维NSArray。 But after use that values their IDs not show as Integers.And I tried @[i,j] too but it didn't work. 但是使用该值后,其ID不会显示为Integers。我也尝试了@[i,j]但没有用。 Here is my code : 这是我的代码:

resault = [[NSMutableArray alloc]initWithCapacity:40];

for (int i=0; i<5; i++)
{
   for (int j=0; j<5; j++)
   {
      [resault addObject:@[@(i),@(j)]];
   }
}

Use this code - 使用此代码-

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

You can try this: 您可以尝试以下方法:

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);

Cycle through the I loop, then the D loop and add into the array as NSString objects. 在I循环中循环,然后在D循环中循环,并作为NSString对象添加到数组中。 If you need them as NSNumbers, just convert them later. 如果您需要将它们用作NSNumbers,则稍后再进行转换。 If you are counting up from 0 to n, you can use a single for loop (eg from 0 to 100 would work in the example above), while nesting for loops works better if you're using non numerical IDs. 如果从0到n递增计数,则可以使用一个for循环(例如,在上面的示例中可以从0到100进行计数),而如果使用非数字ID,则嵌套for循环会更好。

If you want to keep the 0 character in front of IDs below ten, using a single for loop: 如果要将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);

Using your method: 使用您的方法:

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

You can then access the i,j pairs by running through the result array and extracting the stored NSArray and getting the first and second objects sorted therein. 然后,您可以通过遍历结果数组并提取存储的NSArray并在其中排序第一个和第二个对象来访问i,j对。

First of all you have to understand the int, float these basic types can not be deposited into the array 首先您必须了解int,float这些基本类型不能被存入数组

So when you add it @(i), this means [NSNumber numberWithInt: i] 因此,当您将其添加到@(i)时,这意味着[NSNumber numberWithInt:i]

The array is the type of NSNumber 数组是NSNumber的类型

If you want to take it out 如果你想拿出来

int result = [resault [30] [0] intValue];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM