Add to NSMutableArray

I have 2 doublings with each iteration that I want to add in NSMutableArray, but I can't seem to 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 it comes from, this is not the value of any of the variables. And Xcode complains about the first two lines, the "local touchArray declaration hides the instance variable. I know that I am doing at least one thing wronl

Thanks for any help, Robert

+3
source share
3 answers

firstly, the problem of local declaration: you already declared touchArray elsewhere, so you do not need to update, just (possibly) initialize like this: touchArray = [[NSMutableArray alloc] init];

-: : : , . NSNumber:

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

, , , . : NSLog(@"array[0] = %g", [[touchArray objectAtIndex:0] doubleValue]

edit : , , . , , , :)

, , , c/iphone, .

+6

+stringWithFormat: . . . "%d", 1 . , "%d" 14108, .

:

[NSString stringWithFormat:@"%g %g", tapTotal, touchBegin]

(, %d β†’ int, %u β†’ unsigned, %g < t29 > CGFloat, %@ β†’ Objective-C ..),

+5

"local declaration of 'touchArray' hides instance variable" , . .h

NSLog , . (.. 0), :

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

+1

Source: https://habr.com/ru/post/1771980/


All Articles