Reputation: 2035
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
Reputation: 2098
Use the following method:
+ (NSNumber *)numberWithInt:(int)value
For example:
NSNumber *object = [NSNumber numberWithInt:1];
Upvotes: 4
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
Reputation: 53970
int myInt = 10;
NSNumber * myNumber = [ NSNumber numberWithInt: myInt ];
See the NSNumber documentation:
Upvotes: 2