How do I make an array of CGFloats in objective c?

meman32 picture meman32 · Jul 8, 2010 · Viewed 21k times · Source

So I'm working on a simple iPhone game and am trying to make a local high score table. I want to make an array and push the highest scores into it. Below is the code I have so far:

    CGFloat score;
    score=delegate.score;
    NSInteger currentindex=0;
    for (CGFloat *oldscore in highscores)
    {
        if (score>oldscore)
        {
            [highscores insertObject:score atIndex:currentindex]
            if ([highscores count]>10)
            {
                [highscores removeLastObject];  

            }

        }
        currentindex+=1;
    }

The problem is that highscores is an NSMutableArray, which can only store objects. So here's my question, what is the best way to store CGFloats in an array? Is their a different type of array that supports CGFloats? Is their a simple way to turn a CGFloat into an object?

And please don't comment on the fact that I'm storing scores in the app delegate, I know it's a bad idea, but I'm not in the mood to have to make a singleton now that the apps almost done.

Answer

Tom Irving picture Tom Irving · Jul 8, 2010

You can only add objects to an NSArray, so if you want you add CGFloats to an array, you can use NSNumber.

Eg:

NSNumber * aNumber = [NSNumber numberWithFloat:aFloat];

Then to get it back:

CGFloat aFloat = [aNumber floatValue];