Ali
Ali

Reputation: 2035

convert int to nsumber*

I have

NsNumber *object = nill;

I want to set object = 1

it raise error in conversion

how to convert int to nsnumber and want to increment the nsnumber by 1

best regards

Upvotes: 1

Views: 414

Answers (4)

dhirschl
dhirschl

Reputation: 2098

Use the following method:

+ (NSNumber *)numberWithInt:(int)value

For example:

NSNumber *object = [NSNumber numberWithInt:1];

Upvotes: 4

Sean
Sean

Reputation: 5820

Alternatively, you can use NSUInteger instead of an NSNumber:

NSUInteger myint = 1;    //declare integer "myint" and set to 1
myint++;                 //increment myint

Note that when declaring a NSUInteger, you do not put a * after the type since it is not an object, but rather a typedef that describes a unsigned integer.

As bbum noted below, since NSUInteger is not an object, it cannot be used in places where an object is required (dictionaries, etc.), so this may or may not be a solution to your problem depending on context.

Upvotes: 0

Mahesh
Mahesh

Reputation: 34625

I want to set object = 1

object = [ NSNumber numberWithInt:1 ] ;

Upvotes: 1

Macmade
Macmade

Reputation: 53970

int myInt = 10;
NSNumber * myNumber = [ NSNumber numberWithInt: myInt ];

See the NSNumber documentation:

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/Reference/Reference.html

Upvotes: 2

Related Questions