Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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
share|improve this question
    
If time is an integer why do you need timeNum which is also an integer and is equal to time? –  Mk12 Aug 18 '09 at 17:15
    
I dont know objective c and cocoa well. I thought that maybe the NSInteger object would work better than the objective c int. –  Bryan Aug 18 '09 at 18:01
    
I made all changes in the answers below but I am still getting the exact same result. –  Bryan Aug 18 '09 at 18:02
    
I think it my have something to do with the index not being initialized yet. In many languages one specifies the size of the array at initialization. –  Bryan Aug 18 '09 at 18:05

3 Answers 3

up vote 4 down vote accepted

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.

share|improve this answer
3  
Storing them as NSNumbers is much simpler -- it's just [NSNumber numberWithInt: <int value>] . Getting the value back is just [((NSNumber*)[array objectAtIndex: <index>]) intValue] . –  Amagrammer Aug 18 '09 at 18:09
    
You did change "myMutableArrayName" to "theResultArray" right? Noticed in the code you pasted that you still had my sample name. No advantage to using NSString unless you are going to be doing string comparisons/functions on the data at certain points in the program. If not, follow Amagrammer's advice in this comment thread. –  Mark Aug 18 '09 at 19:05
    
Hmmm. . .interesting. If you still have the explicit alloc/init in your view did load, try this to test if anything is in your array: NSLog(@"Count of array is: %d", theResultArray.count); If it returns a number greater than 0, that means you are storing things in the array after all. –  Mark Aug 18 '09 at 19:46
    
I finally got it. I had to initialize the array to a default value at all indexes first. theResultArray = [[NSMutableArray alloc]init]; for( int i = 0; i < 50; i++){ [theResultArray insertObject:[NSNumber numberWithInt:0] atIndex:i]; } –  Bryan Aug 18 '09 at 22:55
    
Amagrammer: no need to cast, you can call any selector on an id –  rpetrich Aug 19 '09 at 1:00

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.

share|improve this answer
    
Thanks for the suggestions. I took your advise but I am still getting the same result. Please see the updated code and let me know of any more suggestions you may have. I am completely new to cocoa building my first quiz app. –  Bryan Aug 18 '09 at 17:18
    
The code in your question looks the same to me. Are you getting compilation errors? –  peterb Aug 18 '09 at 17:26
    
No compilatation errors. Just 0 value output for theResultArray at rightIndex. –  Bryan Aug 18 '09 at 17:44
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?

share|improve this answer
    
Time is an objective c int. Right index is the index I want the object added at. Reso is commented out because it is not used. I have 50 questions asked randomly and the result is stored in the corresponding index for the question. –  Bryan Aug 18 '09 at 17:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.