1

I have a NSMutableArray and want to add a 2 dimensional NSArray with integer parameters to it each time. But after use that values their IDs not show as Integers.And I tried @[i,j] too but it didn't work. 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)]];
   }
}
10
  • What is the ouput you are getting and what do you expect? Commented Aug 21, 2017 at 9:17
  • @user1000 resault[0][0] return a ID not Integer Commented Aug 21, 2017 at 9:18
  • @SaMiGiMiX still not sure to understand what you want to return, use real value to express it Commented Aug 21, 2017 at 9:21
  • According to above code you are storing an NSNumber in a slot, so to get integer, you need to use [resault[0][0] integerValue] Commented Aug 21, 2017 at 9:22
  • Please show the example output you want Commented Aug 21, 2017 at 9:24

4 Answers 4

1

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];
}
Sign up to request clarification or add additional context in comments.

1 Comment

I want to count my resault later but with this code I can't .. .
1

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. If you need them as NSNumbers, just convert them later. If you are counting up from 0 to n, you can use a single for loop (e.g. from 0 to 100 would work in the example above), while nesting for loops works better if you're using non numerical IDs.

If you want to keep the 0 character in front of IDs below ten, using a single for loop:

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

1 Comment

i think it's a smarter way, tnx
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]
       ];
   }
}

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.

1 Comment

tnx it's another way
1

First of all you have to understand the int, float these basic types can not be deposited into the array

So when you add it @(i), this means [NSNumber numberWithInt: i]

The array is the type of NSNumber

If you want to take it out

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

1 Comment

tnx for help it's true

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.