Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Passing integers into an array.

This is actually pretty simple and i may be over thinking but I'm having trouble passing in some integers into an array without Xcode throwing any errors in my face.

NSMutableArray *scores = [[NSMutableArray alloc]initWithObjects:_redScore, _Bluescore, _purpleScore, _greenScore, nil];

is what i have s far but Xcode keeps telling me that implicit conversion from int to id is incompatible because of arc.

i just want to be able to have these integers into an array so i can't sort them from higest lowest.

Any suggestions would be great.

3 Answers

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Hello Rashii,

If the scores are integers, the issue is initWithObjects is expecting objects instead of primitive types.

Here is an example of how this could be done.

int x = 1;
int y = 2;

NSArray *array = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:x], [NSNumber numberWithInt:y], nil];

I hope this helps.

one last question Justin, would i be able to use NSSortDescriptor to order those integers from highest to lowest?

Justin Horner
Justin Horner
Treehouse Guest Teacher

Yes, you can use NSSortDescriptor to sort in descending order like this.

NSSortDescriptor *mySorter = [[NSSortDescriptor alloc] initWithKey:@"intValue" ascending:NO];
array = [array sortedArrayUsingDescriptors:[NSArray arrayWithObjects:mySorter, nil]];

how will the sort descriptor know what key to initialize with if i didn't store the integers with a key?

Justin Horner
Justin Horner
Treehouse Guest Teacher

When the array is being sorted it will use the intValue function on NSNumber instances. That is the value it then uses for the sort.

I hope this helps.

thanks justing, you're helping a lot and I'm learning a lot.

here's what i implemented, however, i don't get the values in the console although i do get somethings:

NSArray *array = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:_redScore],[NSNumber numberWithInt:_Bluescore],[NSNumber numberWithInt:_purpleScore],[NSNumber numberWithInt:_greenScore], nil];

    NSSortDescriptor *mySorter = [[NSSortDescriptor alloc] initWithKey:@"intValue" ascending:NO];
    array = [array sortedArrayUsingDescriptors:[NSArray arrayWithObjects:mySorter, nil]];


    NSLog(@"%@", mySorter);

Now look here's what i've been getting in the console:

 (intValue, descending, compare:)
2014-06-16 00:25:31.850 Flocking Birds[7449:60b] (intValue, descending, compare:)

array = [array sortedArrayUsingDescriptors:[NSArray arrayWithObjects:mySorter, nil]]; The sorted values are not being stored in mySorter but in the array variable.