[英]How to add an integer value into NSMutableArray?
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;
希望這可以幫助。
添加一個數字。
NSNumber* foo = [NSNumber numberWithInteger:42];
或使用盒裝文字:
NSNumber* foo = @(42);
然后添加foo。
將Int轉換為NSNumber,然后添加到您的數組
問題出在你的NSLog
字符串上
NSLog(@"%d", val);
%d
實際上是integer (int)
的格式說明符。 您正在傳遞一個NSMutableArray對象。
將NSLog
更改為
NSLog(@"%@", val);
%@
期望一個對象,這通常會打印出[myObject description]
,在這種情況下是對val數組的描述。
這是一個簡單的方法: @(yourInt)
int value = 5;
NSMutableArray * anArray = [[NSMutableArray alloc] init];
[anArray addObject: @(value)];
有關at-compiler-directives的更多信息
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.