如何在 Objective C 中创建 CGFloats 数组?
-
13-09-2020 - |
题
所以我正在开发一个简单的 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 转变为对象的简单方法吗?
请不要评论我将分数存储在应用程序委托中的事实,我知道这是一个坏主意,但既然应用程序即将完成,我就没有心情必须创建一个单例。
解决方案
您只能将对象添加到 NSArray
, ,所以如果你想添加 CGFloat
s 到一个数组,你可以使用 NSNumber
.
例如:
NSNumber * aNumber = [NSNumber numberWithFloat:aFloat];
然后把它找回来:
CGFloat aFloat = [aNumber floatValue];
其他提示
CGFloat
i.e。原始阵列可以像...
CGFloat colors[] = { 1, 0, 0, 1 };
.
并随后以类似于...的方式“使用”。
CGContextSetFillColor(context, colors);
.
或(更简洁地,通过铸造)...
CGContextSetFillColor(context, (CGFloat[]){ 1, 0, 0, 1 });
.
C
arrays gross 我出局......但这可以用“标准©”!
不隶属于 StackOverflow