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 > My strings won't append!

My strings won't append!
Thread Tools
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
May 24, 2003, 10:07 AM
 
For some reason I can't get a string to append to another string. Here's the code:

Code:
-(NSCalendarDate *) setTimeLastWUCompleted:(NSString *)time { NSString *dateAndTime=@"1972-01-03 "; NSLog(@"time=%@",time); [dateAndTime stringByAppendingString:time]; NSLog(@"dateAndTime=%@",dateAndTime); [timeLastWUCompleted release]; timeLastWUCompleted = [[[NSCalendarDate alloc] initWithString:dateAndTime calendarFormat:@"%y-%m-%y %H:%M:%S"] autorelease]; }
Here's the output:

Code:
2003-05-24 08:01:34.585 F@HMonitor[771] time=14:39:01 2003-05-24 08:01:34.585 F@HMonitor[771] dateAndTime=1972-01-03 2003-05-24 08:01:34.586 F@HMonitor[771] timeLastWUCompleted=(null)
The NSCalendarDate isn't being set properly because the string doesn't conform to the format because the time isn't being appended to dateAndTime. Any ideas why this isn't working?

thanks,
kman
     
Senior User
Join Date: Jan 2003
Location: Stuttgart, Germany
Status: Offline
Reply With Quote
May 24, 2003, 10:48 AM
 
Despite of me being more of a Lisp guy, I think it's safe to say that the code looks a bit fishy.

First, are you allowed to destructively modify a constant string? Shouldn't you be allocating a fresh NSString object?

Second, is stringByAppendingString supposed to be used destructively? A quick glance at the docs seems to suggest otherwise: from what I gathered it returns a NSString object rather than modifying its receiver.

Try saving the result instead of discarding it: NSString *foo = [dateAndTime stringBy...];
     
Forum Regular
Join Date: May 2001
Location: Oviedo, Floriduh USA
Status: Offline
Reply With Quote
May 24, 2003, 11:22 AM
 
Remember that NSString is immutable (cannot be changed) and that NSMutableString is available just for that purpose.
folding@home is good for you.
     
Mac Elite
Join Date: Sep 2000
Location: Norfolk, Va
Status: Offline
Reply With Quote
May 24, 2003, 11:42 AM
 
I haven't tested it, but I think you need to reassign the return to dateAndTime, a la
Code:
dateAndTime = [dateAndTime stringByAppendingString:time];
and for that dateAndTime must be a NSMutableString.

Yet another time when NSString should be NSImmutableString, eh?
you are not your signature
     
kman42  (op)
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
May 24, 2003, 12:11 PM
 
Thanks! That was indeed the problem. Interestingly, I didn't have to use NSMutableString. Go figure...

Code:
-(NSCalendarDate *) setTimeLastWUCompleted:(NSString *)time { time=[time stringByAppendingString:@" +0000"]; NSString *dateAndTime=@"1972-01-03 "; dateAndTime=[dateAndTime stringByAppendingString:time]; [timeLastWUCompleted release]; timeLastWUCompleted = [[NSCalendarDate alloc] initWithString:dateAndTime calendarFormat:@"%Y-%m-%d %H:%M:%S %z"]; [timeLastWUCompleted setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"PST"]]; NSLog(@"timedescription=%@",[timeLastWUCompleted description]); }
Output:
Code:
2003-05-24 10:13:27.626 F@HMonitor[1510] timedescription=1972-01-03 08:13:05 -0800
It's always the silly things when your a newbie.

kman
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
May 24, 2003, 01:07 PM
 
Just to clarify, this is the expected behavior.
- (NSString *)stringByAppendingString:(NSString *)aString

Returns a string object made by appending aString to the receiver. This code excerpt, for example:

NSString *errorTag = @"Error: ";
NSString *errorString = @"premature end of file.";
NSString *errorMessage = [errorTag stringByAppendingString:errorString];


produces the string "Error: premature end of file.".
If you want to modify the original string, you'll need to use NSMutableString's -appendString: method.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Senior User
Join Date: Nov 2001
Location: State of Denial
Status: Offline
Reply With Quote
May 24, 2003, 07:40 PM
 
You didn't have to use an NSMutableString because it doesn't modify the string object, it returns a new string with another string appended to it—which is also why you had to store the return value in another NSString for it to do anything constructive.

Edit: crap, posted too soon...I didn't read the above post, which explains it as well...
[Wevah setPostCount:[Wevah postCount] + 1];
     
   
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:35 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