所以我正在开发一个简单的iphone游戏,并试图制作一个本地高分表。我想做一个数组,把最高的分数加进去。以下是我目前掌握的代码:
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;
}
问题是highscores是一个nsmutablearray,它只能存储对象。所以我的问题是,在数组中存储cgfloat的最佳方法是什么?它们是支持cgfloat的不同类型的数组吗?它们是将cgfloat转换为对象的简单方法吗?
请不要评论我在应用程序代理中存储分数的事实,我知道这是一个坏主意,但现在应用程序几乎完成了,我没有心情去做一个单独的项目。
最佳答案
您只能将对象添加到NSArray
,因此如果要将CGFloat
s添加到数组,可以使用NSNumber
。
如:
NSNumber * aNumber = [NSNumber numberWithFloat:aFloat];
然后要拿回它:
CGFloat aFloat = [aNumber floatValue];