gpt4 book ai didi

iphone - 如何将整数值添加到 NSMutableArray?

转载 作者:太空狗 更新时间:2023-10-30 03:15:12 26 4
gpt4 key购买 nike

NSMutableArray * val;
val = [[NSMutableArray alloc] initWithCapacity:15];

/*int outlineWidth = barOutlineWidth;
int outlineHalfWidth = (outlineWidth > 1) ? outlineWidth * 0.5f : 0;
*/
for ( Bar * obj in values )
{
// calcualte the bar size
float value = [obj value];
float scale = ( value / maxValue );

// shift the bar to the top or bottom of the render line
int pointY = lineHeight + ( (lineWidth * 0.5f) * ( ( value >= 0.0f ) ? -1 : 1 ) );
int barHeight = height * scale;
NSLog(@"%d", barHeight);
CGRect barRect = CGRectMake(pointX, pointY, width, -barHeight);
[val addObject:[NSNumber numberWithInt:barHeight]];
NSLog(@"%d", val);

我想将 barheight (int) 添加到数组 val 中。这可能吗?在运行代码时,

session started at 2010-09-16 13:21:50 +0530.]
2010-09-16 13:21:53.791 BarGraphSample[3168:20b] 78
2010-09-16 13:21:53.797 BarGraphSample[3168:20b] 69398112
2010-09-16 13:21:53.807 BarGraphSample[3168:20b] 235
2010-09-16 13:21:53.812 BarGraphSample[3168:20b] 69398112
2010-09-16 13:21:53.813 BarGraphSample[3168:20b] 156
2010-09-16 13:21:53.814 BarGraphSample[3168:20b] 69398112

这是输出。

这里实际的条高是 78,235,156,在打印数组 val 时。

我得到像“69398112”这样的值

我该怎么办?

最佳答案

您只能将指向对象的指针添加到 NSMutableArray。如果你使用 NSNumber 类来包装你的整数,那么你应该能够将它添加到数组中。

int x = 10;
NSNumber* xWrapped = [NSNumber numberWithInt:x];
NSMutableArray* array = [[NSMutableArray alloc] initWithCapacity:15];
[array addObject:xWrapped];
int xOut = [[array lastObject] intValue]; //xOut == x;

希望这对您有所帮助。

关于iphone - 如何将整数值添加到 NSMutableArray?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3724757/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com