0

please tell me that can we create an array of imteger in objective c i am a new iphone programmer and i am creating an aray which can take an integer from a view (at 0th position)and pass that array to next view and then again put an integer to it (at 1st position) and so on.... please help because it does not accept integer value

Ranjeet Sajwan
  • 1,925
  • 2
  • 28
  • 60

2 Answers2

3

What you want to do is add NSNumbers to the array.

[myArray addObject:[NSNumber numberWithInt:myInteger]];

And to access the integer from the array

int myInteger = [[myArray objectAtIndex:myIndex] intValue];
Alex Nichol
  • 7,512
  • 4
  • 32
  • 30
2

You can only store objects in NSArrays, not primitives (like int, float, NSInteger, etc.), which is probably what you're trying. Use NSNumber instead.

Shaggy Frog
  • 27,575
  • 16
  • 91
  • 128
  • but if i use NSNumber *myInt = [NSNumber numberWithInt:sender.tag]; [myarray addObject:myInt]; it show null on oth position of my array i have already declared my array no compilation problen is there...... – Ranjeet Sajwan Aug 06 '10 at 05:22
  • You'll have to rephrase your question. I am having trouble understanding. If you are trying to add objects to an `NSArray` after initialization, that will not work, as it is not a *mutable* class. You will want to use `NSMutableArray` instead. – Shaggy Frog Aug 06 '10 at 05:37