Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

int / float to NSString without using alloc?

Is there anyway to get int (or float) numbers into a NSString object without using alloc and a subsequent release?

int myInt = 25;
NSString *myString = [[NSString alloc] initWithFormat:@"%d",myInt];
... 
[myString release];

EDIT:

Thanks for the answers, I should have been a little more clear in the question, I am particularly interested in using this on the iPhone. As @chuck stated I could use a convenience method, but I was under the impression that I should be avoiding these where possible on the iPhone for memory / performance reasons. I could be wrong though.

gary

like image 788
fuzzygoat Avatar asked Nov 28 '22 23:11

fuzzygoat


1 Answers

There's no way to create an NSString without creating it at some point. But you could use a convenience constructor so you don't have the burden of ownership.

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

It will still be created and destroyed (as everything must be), but you don't have to do it yourself.

like image 118
Chuck Avatar answered Dec 16 '22 11:12

Chuck