PDA

View Full Version : create array of numbers




henkhenk
Nov 2, 2009, 09:45 AM
hi all:

I want to keep track of some numbers. for this purpose I will need to use an array of numbers:

should I create it and use it the following way or there is an easy way?

NSMutablearray *array1;

[array1 addObject:[NSString stringWithFormat:@"%d", count]];



robbieduncan
Nov 2, 2009, 09:50 AM
If they are really numbers why not use NSNumber (http://developer.apple.com/mac/library/documentation/cocoa/Reference/Foundation/Classes/NSNumber_Class/Reference/Reference.html) instead of NSString?

Or, if you know the size in advance, why not just use a C array?

henkhenk
Nov 2, 2009, 10:01 AM
If they are really numbers why not use NSNumber (http://developer.apple.com/mac/library/documentation/cocoa/Reference/Foundation/Classes/NSNumber_Class/Reference/Reference.html) instead of NSString?

Or, if you know the size in advance, why not just use a C array?

hi robbieduncan tanks for the replay.

I dont know the size in advance. and if I will add the number to the array like this way:
[array1 addObject:[NSString stringWithFormat:@"%d", count]];

how I can read it back and assign it to an NSIntiger or int? (like if I want to select one value from the array by an objectIndex.)

tanks
henk

robbieduncan
Nov 2, 2009, 10:03 AM
As per my above suggestion: don't store it like that. If it's a number don't convert it to a string. You are just asking for pain. Store it as a NSNumber.

henkhenk
Nov 2, 2009, 10:10 AM
As per my above suggestion: don't store it like that. If it's a number don't convert it to a string. You are just asking for pain. Store it as a NSNumber.

I had some problem with NSNumber.

ok I will try it.

tanks

henkhenk
Nov 2, 2009, 02:41 PM
I had some problem with NSNumber.

ok I will try it.

tanks
problem solved:

to ad an int to NSMutableArray:
int count;
NSMutableArray *array1;
NSNumber *mynum =[NSNumber numberWithInteger:count];
[array1 addObject:mynum];

to covert NSNumber in NSMutableArray to int:
int x = [[array1 objectAtIndex:indeX] intValue];