开发者

how to convert integer to string? [duplicate]

开发者 https://www.devze.com 2023-01-10 20:31 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: How to convert from int to string in objective c: example code…
This question already has answers here: Closed 12 years ago.

Possible Duplicate:

How to convert from int to string in objective c: example code…

How to conve开发者_如何学Pythonrt integer to string in Objective C?


NSArray *myArray = [NSArray arrayWithObjects:[NSNumber numberWithInt:1], [NSNumber numberWithInt:2], [NSNumber numberWithInt:3]];

Update for new Objective-C syntax:

NSArray *myArray = @[@1, @2, @3];

Those two declarations are identical from the compiler's perspective.

if you're just wanting to use an integer in a string for putting into a textbox or something:

int myInteger = 5;
NSString* myNewString = [NSString stringWithFormat:@"%i", myInteger];


If you really want to use String:

NSString *number = [[NSString alloc] initWithFormat:@"%d", 123];

But I would recommend using NSNumber:

NSNumber *number = [[NSNumber alloc] initWithInt:123];

Then just add it to the array.

[array addObject:number];

Don't forget to release it after that, since you created it above.

[number release];


NSString* myNewString = [NSString stringWithFormat:@"%d", myInt];
0

精彩评论

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

关注公众号