Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Stupid string question

Stupid string question
Thread Tools
Mac Enthusiast
Join Date: Apr 2001
Location: Newton, MA, USA
Status: Offline
Reply With Quote
Apr 9, 2003, 10:49 AM
 
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
Reply With Quote
Apr 9, 2003, 11:18 AM
 
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
Reply With Quote
Apr 9, 2003, 11:54 AM
 
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
Reply With Quote
Apr 9, 2003, 02:48 PM
 
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'."
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 03:22 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2