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
这是输出。
在这里,当打印数组val时,实际的barheight是78,235,156。
我越来越喜欢像"69398112“这样的值了
我该怎么办?
发布于 2010-09-16 16:04:07
您只能将指向对象的指针添加到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;
希望这能有所帮助。
发布于 2010-09-16 16:02:08
改为添加一个数字。
NSNumber* foo = [NSNumber numberWithInteger:42];
或者使用盒装文字:
NSNumber* foo = @(42);
然后添加foo。
发布于 2010-09-17 23:09:10
问题出在NSLog
字符串上,即
NSLog(@"%d", val);
%d
实际上是integer (int)
的格式说明符。您正在向它传递一个NSMutableArray对象。
将NSLog
更改为
NSLog(@"%@", val);
%@
需要一个对象,这通常会打印出[myObject description]
,在本例中是val数组的描述。
https://stackoverflow.com/questions/3724757
复制相似问题