 |
 |
My strings won't append!
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status:
Offline
|
|
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
|
|
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
|
|
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
|
|
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 NS ImmutableString, eh? 
|
|
you are not your signature
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status:
Offline
|
|
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
|
|
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
|
|
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];
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
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
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|