开发者

convert int to nsumber*

开发者 https://www.devze.com 2023-02-22 06:07 出处:网络
I have开发者_StackOverflow社区 NsNumber *object = nill; I want to set object = 1 it raise error in conversion

I have开发者_StackOverflow社区

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


Use the following method:

+ (NSNumber *)numberWithInt:(int)value

For example:

NSNumber *object = [NSNumber numberWithInt:1];


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


I want to set object = 1

object = [ NSNumber numberWithInt:1 ] ;


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号