Bryan
Bryan

Reputation: 17581

Adding Objects to an Objective C Array at Index

I have a synthesized NSMutableArray - theResultArray . I want to insert NSNumber or NSInteger objects at specific indexes (0-49). For some reason I can never get any values to stick in my array. Every index returns nil or 0.

    NSInteger timeNum = time;
    [theResultArray insertObject:[NSNumber numberWithInt:timeNum] atIndex:rightIndex];
    NSLog(@"The right index is :%i", rightIndex);
    NSLog(@"The attempted insert time :%i", time);
    NSNumber *testNum = [theResultArray objectAtIndex:rightIndex];
    NSLog(@"The result of time insert is:%i", [testNum intValue]);

I alloc-init theResultsArray in viewDidLoad. Time is an integer. I have been trying different combinations of the code above to no avail.

The console outputs this:

StateOutlineFlashCards[20389:20b] The right index is :20
StateOutlineFlashCards[20389:20b] The attempted insert time :8
StateOutlineFlashCards[20389:20b] The result of time insert is:0

Upvotes: 1

Views: 30650

Answers (3)

Mark
Mark

Reputation: 2071

You need to allocate memory for the array in your init or viewDidLoad method, otherwise you won't be able to store anything at all.

If you do this:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization        
        myMutableArrayName = [[NSMutableArray alloc] init];
    }
    return self;
}

Or this:

- (void)viewDidLoad {
    [super viewDidLoad];
    myMutableArrayName = [[NSMutableArray alloc] init];
}

It should work for you.

As for storing integers in an NSMutableArray, I took a simple but somewhat "hackish" approach recently. I store them as strings. When I put them in I use:

[NSString stringWithFormat:@"%d", myInteger];

And when I take them out I convert with:

[[myArray objectAtIndex:2] intValue];

It was really easy to implement but depending on the context you may want to use another way.

Upvotes: 4

peterb
peterb

Reputation: 1378

Unless I'm misreading, aren't you inserting an NSInteger, but then trying to take out an NSNumber? Those are two completely different data types. It doesn't surprise me that you're getting odd results.

Furthermore, NSInteger isn't an object, so you can't stick it into an array. You probably want to be allocating an NSNumber with that integer and putting that in.

Try something like: [theResultArray addObject:[NSNumber numberWithInteger:timeNum] atIndex:rightIndex];

Likewise, when you retrieve the value, you'll need to unbox it:

NSLog(@"The result of time insert is:%i", [testNum integerValue])`;

Likewise, when you retrieve the value, you'll need to unbox it:

Frankly, I'm a little surprised that this even compiles.

Upvotes: 5

mk12
mk12

Reputation: 26374

NSInteger timeNum = time;

What is that for? What is "time"?

    [theResultArray addObject:timeNum atIndex:rightIndex];

There is no method -addObject:atIndex:. It is -insertObject:atIndex:. Why are you inserting at "rightIndex" anyway? Why not just use -addObject:?

    //[theResultArray replaceObjectAtIndex:rightIndex withObject:[NSNumber numberWithInt:timeNum]];

What is that and why is it commented out?

    NSLog(@"The right index is :%i", rightIndex);
    NSLog(@"The attempted insert time :%i", time);
    NSNumber *testNum = [theResultArray objectAtIndex:rightIndex];
    //int reso = [testNum integerValue];
    NSLog(@"The result of time insert is:%i", testNum);

What are you trying to do?

Upvotes: 1

Related Questions