 |
 |
Stupid string question
|
 |
|
 |
|
Mac Enthusiast
Join Date: Apr 2001
Location: Newton, MA, USA
Status:
Offline
|
|
i'm wading my way out of the shallow end of the pool with Cocoa these days, and have run into something that kinda vexes me a little, and i'm sure i'm doing something dumb, but would like to know what it is.
In lots of contexts, you can specify an integer value to be substituted into a string such as the following:
NSLog(@"Processing line %d of %d.", currentLine, numLines);
so that you get the string "Processing line 22 of 47." or whatever. That works great for NSLog, and NSRunAlertPanel and stuff like that, but i can't figure out how to do the same thing with an NSString separately, something like this:
foo=@"Processing line %d of %d.", currentLine, numLines; or
foo=(@"Processing line %d of %d.", currentLine, numLines); or
foo=[NSString stringWithFormat:@"...%d of %d", currentLine, numLines];
and so on, with, without parentheses (with parentheses seems to lead to error 'integer from pointer without cast' --> crash), whatever, i've tried all kinds of things. Can't figure this one out, but then, i am terrible at this. Is this even possible? Surely there must be some way to shove an int value into an NSString, but i couldn't find it in any of the documentation.
Any insight would be greatly appreciated; thanks for listening...
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Mar 2000
Location: Ithaca, NY
Status:
Offline
|
|
Originally posted by calamar1:
foo=[NSString stringWithFormat:@"...%d of %d", currentLine, numLines];
This is the one you want to use, however, you should be aware that since this is not an alloc/init you're calling, that you need to retain the string that's returned in order to keep it around. So you want either:
foo = [[NSString stringWithFormat:@"...%d of %d", currentLine, numLines] retain];
or
foo = [[NSString alloc] initWithFormat:@"...%d of %d", currentLine, numLines];
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Apr 2001
Location: Newton, MA, USA
Status:
Offline
|
|
Originally posted by bewebste:
This is the one you want to use, however, you should be aware that since this is not an alloc/init you're calling, that you need to retain the string that's returned in order to keep it around. So you want either:
foo = [[NSString stringWithFormat:@"...%d of %d", currentLine, numLines] retain];
or
foo = [[NSString alloc] initWithFormat:@"...%d of %d", currentLine, numLines];
Thank you so much! That did the trick!
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Originally posted by bewebste:
So you want either:
foo = [[NSString stringWithFormat:@"...%d of %d", currentLine, numLines] retain];
or
foo = [[NSString alloc] initWithFormat:@"...%d of %d", currentLine, numLines];
Incidentally, the second is much more efficient. The first one basically expands to [[[[NSString alloc] initWithFormat:@"...%d of %d", currentLine, numLines] autorelease] retain], plus the overhead of the initial message.
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|