views:

38

answers:

3

I have NSInteger variable, for example NSInteger example=1256 and i need an array with elements of this variable.

so first element of array is array[0] = 1
array[1] = 2
array[2] = 5 etc.. 

what way can i solve it ?

A: 

You need to use NSMutableArray to be able to change entries. NSMutableArray can only hold objects, not primitive types like NSInteger. Also, if you are using NSMutableArray, you can't access the elements the same way as with a C array.

  [array insertObject:[NSNumber numberWithInteger:2] atIndex:1];
Steven XM
A: 

You can convert your integer to a char* then iterate through it casting each character back to an int and adding it to a C array or, as Steven says, an NSArray of NSNumbers.

Ben
+1  A: 

Here's about how I'd do it:

NSUInteger number = 1234567890;
NSMutableArray * numbers = [NSMutableArray array];
while (number > 0) {
  NSUInteger lastDigit = number % 10;
  [numbers insertObject:[NSNumber numberWithUnsignedInteger:lastDigit] atIndex:0];
  number = number / 10;
}
Dave DeLong