17

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.

meman32
  • 766
  • 2
  • 6
  • 9

2 Answers2

31

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];
Tom Irving
  • 10,041
  • 6
  • 47
  • 63
  • 6
    With Xcode 4.5, this can be condensed to '@(floatVariable)' to get your NSNumber. For example, 'NSNumber * aNumber = @(4.65);' – WDUK Nov 27 '12 at 23:20
  • 1
    This is NOT safe! It may work now that the iDevices are still using a 32-bit architecture, but once they move to 64-bit in a couple of years this will break. The only safe way is to use CFNumber objects with kCFNumberCGFloatType. – JanX2 Jun 05 '13 at 12:35
  • @JanX2 Actually not in "couple of years". Apple presented iPhone 5s with A7 chip with 64bit architecture in September 10 2013. – Paweł Brewczynski Nov 05 '13 at 11:00
  • @bluesm I estimated they would take until 2014. I was overly pessimistic. ;) – JanX2 Nov 05 '13 at 16:55
  • @JanX2 I totally agree with you. For a rigorous attitude to the code, we should not replace CGFloat with float. Checked the definition of CGFloat, it shows that with 64bit architecture, CGFloat is double and with 32bit architecture, CGFloat is float. – ManuQiao Dec 29 '14 at 02:37
13

CGFloat i.e. primitive arrays can be constructed like so...

CGFloat colors[] = { 1, 0, 0, 1 };

and subsequently "used" in a fashion similar to...

CGContextSetFillColor(context, colors);

OR (more concisely, via casting)...

CGContextSetFillColor(context, (CGFloat[]){ 1, 0, 0, 1 });

C arrays gross me out... but this CAN be done with "standard ©"!

Alex Gray
  • 16,007
  • 9
  • 96
  • 118
  • 3
    Easy now. The other answer is not "wrong", more that it's another way to do it (especially convenient if you're using objects all over the place). Your method will be more efficient, but might not be as flexible though out the application as a whole. – WDUK Nov 27 '12 at 23:24
  • This is the way that is hinted at for C based functions like "CGColorCreate()", so thank you for reminding me about this. – Kirbinator Aug 07 '13 at 20:29