tags:

views:

45

answers:

3

I have 2 doubles with each iteration I want add to a NSMutableArray but I can't seem to make it happen.

NSMutableArray *touchArray = [[NSMutableArray alloc]init];
[touchArray addObject:[NSString stringWithFormat:@"%d", "%d", tapTotal, touchBegin]];      
NSLog(@"array: %@", touchArray);

The console prints 14108 as the value for the array, I'm not sure where that's coming from, it's not the value of either on of the variables. And XCode complains on the first two lines, "local declaration of 'touchArray' hides instance variable. I know I'm doing at least one thing wronl

Thanks for any help, Robert

+5  A: 
KennyTM
This is actually *incorrect*: `%d` expects an int, but the value passed is a double. The sizes of these two objects (in the C standard sense of the word) likely differ, and this is likely to screw up the varargs handling. If you want to truncate to int, then you should explicitly cast the double values to int, e.g., `(int)tapTotal`.
Jeremy W. Sherman
+3  A: 

first, the 'local declaration problem': You have already declared touchArray elsewhere, therefore you don't need to redeclare, just (possibly) initialize like so touchArray = [[NSMutableArray alloc] init];

Secondly: the double problem: theres quite a few: you are adding a string to the array, not two doubles. to add a double, you need to wrap it in an NSNumber first:

[touchArray addObject:[NSNumber numberWithDouble:firstDouble];
[touchArray addObject:[NSNumber numberWithDouble:secondDouble];

also you want to print out each variable in the array to check it i would believe, not the array object itself. for this: NSLog(@"array[0] = %g", [[touchArray objectAtIndex:0] doubleValue]

edit for answer clarification: my answer assumes the poster wants to add two doubles seperately to the array, not a string with two values in it. Thought i might clear that up as kenny and I had different outtakes :)

theres a lot wrong here, so i would suggest maybe trying to read a learning objective c/iphone development book.

Jesse Naugher
double would be `%g`.
KennyTM
ahh of course, wasnt thinking just saw it in his post :)
Jesse Naugher
Even easier: print the `NSNumber` object: `NSLog(@"array[0] = %@", [touchArray objectAtIndex:0])`. Easier still: just print he entire array, as the asker suggested: `NSLog(@"array = %@", array)`. `-[NSArray description]` prints out its contents in pseudo-oldstyle-plist format.
Jeremy W. Sherman
+1  A: 

"local declaration of 'touchArray' hides instance variable" shows you already have defined same array anywhere else in the code locally. Or you defined it into your .h file

NSLog cant directly print what you have inside a array. If you added a string as its first object (i.e. index 0) then you should print like this-

NSLog(@"array: %@", [touchArray objectAtIndex:0]);

Cheers

Vaibhav Saran