19
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);

I want to add the barheight (int) into array val. Is this possible? while running the code,

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

this is the output.

Here the actual barheights are 78,235,156, while printing the array val.

Im getting like values like "69398112"

What should I do?

5 Answers 5

50

You can only add pointers to objects to an NSMutableArray. If you use the NSNumber class though to wrap your integer, you should then be able to add that to the array.

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

Hope this helps.

Sign up to request clarification or add additional context in comments.

3 Comments

xOut will be an NSNumber* not an int, unless you also pass intValue to the object returned by lastObject.
By "pointers" I think you mean "objects" -- e.g. adding a char* would still require wrapping it up in an object.
I see your point but I don't completely agree. Firstly, I did say 'pointers to objects'. A char* wouldn't really fall into that as I would say that is a pointer to a primitive type. Secondly, you are adding a pointer to an object to the array. In the example above I add xWrapped, which is an NSNumber pointer.
20

Add a number instead.

NSNumber* foo = [NSNumber numberWithInteger:42];

or using boxed literals:

NSNumber* foo = @(42);

then add foo.

Comments

1

The issue is with your NSLog string namely

NSLog(@"%d", val);

%d is in fact the format specifier for an integer (int). You are passing it a NSMutableArray object.

Change the NSLog to

NSLog(@"%@", val);

The %@ expects an object, and this will typically print out [myObject description], in this case a description of your val array.

Comments

1

Convert Int to NSNumber then add to your array

Comments

1

This is a simple way: @(yourInt)

int value = 5;
NSMutableArray * anArray  = [[NSMutableArray alloc] init];
[anArray addObject: @(value)];

more information about at-compiler-directives

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.