5

Is there a nicer way to fill an array with numbers than what I use? It's crazy how much I got to write just to fill an array with numbers so they can be used for a calculation in a loop. This is easier in other C based languages like PHP, As3, or Java.

NSArray *myArray = [NSArray arrayWithObjects:  
                    [NSNumber numberWithInt:1000],[NSNumber numberWithInt:237], [NSNumber numberWithInt:2673], nil];

int total = 0;
for(int i = 0; i < [myArray count]; i += 1 ){
    total += [[myArray objectAtIndex: i]intValue];
    NSLog(@"%i", total);
}

Hopefully there is a shorter way... I just want to fill an array with ints... cant be that hard

2
  • 2
    None of those languages are "C-based". Commented Jun 24, 2012 at 17:53
  • For some things you want NSNumber objects, they can be stored in collections and automatically encoded/decoded to files/network streams, etc. For other things you may want good old C ints in a C array, for super-fast lookup and usage (for example a frequency table in an audio processing function). In Objective-C you can choose which is the most appropriate, it supports both. Commented Jun 24, 2012 at 18:06

4 Answers 4

10

I guess you have to use NSNumber for an NSArray. If you want to use ints I guess you'd have to use a c array:

NSInteger myArray[20];

for (int i=0;i<20;i++) {
  int num=myArray[i];

  //do something
 }

NSNumber though is I guess the better approach for this language. At least you can do fast enumeration to shorten code a bit:

for (NSNumber *n in myArray) {
 int num = [n intValue];

 //do something....

}

EDIT:

The question has been asked 3 years ago. There have been new literals established to make it easier to create objects like NSNumbers or NSArrays:

NSNumber *n = @100;

or

NSArray *array = @[@100,@50,@10];
Sign up to request clarification or add additional context in comments.

2 Comments

The literal syntax described is already available: clang.llvm.org/docs/ObjectiveCLiterals.html
Sure... its 3 years later :) I edited the answer to make it un-ambiguous.
0

Nice short alternative for looping specific integers:

NSArray *numbers = [@"1000,237,2673" componentsSeparatedByString:@","];
for (NSString *i in numbers) {
    [i intValue]; // Do something.
}

Comments

0

First start with a C array:

NSInteger myCArray = { 1000, 237, 2673 };
// calculate number of elements
NSUInteger myCArrayLength = sizeof(myCArray) / sizeof(NSInteger;

Second, if you need an NSArray loop through this array and create one:

NSMutableArray *myNSArray = [NSMutableArray arrayWithCapacity:myCArrayLength];
for(NSUInteger ix = 0; ix < myCArrayLength; ix++)
   [myNSArray addObject:[NSNumber numberWithInteger:myCArray[ix]];

You can wrap the second piece of code up as a category on NSArray if you're doing it a lot.

Comments

0

too late. but u can do the following too.

int total = 0;
nsarray *myArray = @[@1.8,@100,@299.8]; 
for(nsnumber *num in myArray){
 total+=num;
}

1 Comment

It looks like this syntax is covered in the accepted answer already - what value does putting it again have?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.