如何在 Objective C 中创建 CGFloats 数组?
所以我正在开发一个简单的 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,它只能存储对象。所以这是我的问题,在数组中存储 CGFloats 的最佳方法是什么?它们是支持 CGFloats 的不同类型的数组吗?他们是将 CGFloat 转变为对象的简单方法吗?
请不要评论我将分数存储在应用程序委托中的事实,我知道这是一个坏主意,但既然应用程序即将完成,我就没有心情必须创建一个单例。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只能将对象添加到
NSArray
中,因此如果您希望将CGFloat
添加到数组中,可以使用NSNumber
。例如:
然后将其取回:
You can only add objects to an
NSArray
, so if you want you addCGFloat
s to an array, you can useNSNumber
.Eg:
Then to get it back:
CGFloat
即原始数组可以像这样构造...然后以类似于...
OR(更简洁地,通过转换)...
C
数组恶心我……但这可以用“标准©”来完成!CGFloat
i.e. primitive arrays can be constructed like so...and subsequently "used" in a fashion similar to...
OR (more concisely, via casting)...
C
arrays gross me out... but this CAN be done with "standard ©"!