Astephen2
Astephen2

Reputation: 851

Acquiring NSInteger Value

I am trying to grab the value of an NSInteger and set a UILabel value to it. The code is as follows:

counter_.text = [NSString stringWithFormat:@"%@", [counterAmount intValue]];

where counter_ is my label and counterAmount is my NSInteger. I keep getting a "Receiver type 'NSInteger *' (aka 'int *') is not 'id' or interface pointer, consider casting it to id.

I'm not quite sure how to understand this. I appreciate of your help.

Upvotes: 0

Views: 593

Answers (2)

lnafziger
lnafziger

Reputation: 25740

Since counterAmount is a NSInteger, you can not use intValue on it (since it isn't an object, you can't send any messages to it, actually).

Instead, use:

counter_.text = [NSString stringWithFormat:@"%d", counterAmount];

Now that being said, if you are displaying this value to a user you should really be using a number formatter so that it is formatted the way that they have set it up to display in the settings app:

NSNumber *number                = [NSNumber numberWithInt:counterAmount];

NSNumberFormatter *formatter    = [NSNumberFormatter new];
formatter.numberStyle           = NSNumberFormatterDecimalStyle; // Or whichever style is appropriate for your number.

counter_.text                   = [formatter stringFromNumber:number];

Upvotes: 1

Tommy
Tommy

Reputation: 100652

intValue is an NSNumber method that returns a C primitive value. %@ prints objects. In order to output a C primitive value you have to supply the type in the formatting string. %d is the type for signed integer.

As per the question, and as pointed out to me quite correctly by borrden below, the type being dealt with here is NSInteger rather than NSNumber. In iOS NSInteger is a typedef of int, so you're dealing directly with a primitive type. The NS prefix does not mean that its an object.

Upvotes: 1

Related Questions