This is my string:
2011-10-07T08:55:16-05:00
I am trying to remove the colon with this code:
NSRange range = NSMakeRange(dateString.length-3, 1);
NSString *temp = [dateString substringWithRange:range];
if ([temp isEqualToString:@":"])
[dateString replaceCharactersInRange:range withString:@""];
My code enters the if statement, so I know that it found the colon. But it crashes with no errors on the last line. What am I doing wrong?
dateString
is not retained, and when you createtemp
it stomps ondateString
. (You should show the logic that createsdateString
.) – Hot LickssubstringWithRange:
won't alter the source (dataString
) object, so it won't "stomp". My guess isdataString
is not actually mutable (NSMutableString
). – gschandlerdateString
was not retained (-> released) the code would crash ondateString.length
in line 1. I suspect you referred to an autoreleaseddateString
, but an autoreleased object would most certainly not "get stomped" by any other object. Autoreleased objects are released at the end of the runloop, which means "when your code exit and the app idles" almost all the time. – AhtidateString
. But that other object is being allocated whentemp
is created. Have seen this sort of thing on several occasions. – Hot LicksdateString
has been released but not yet overwritten then, indeed,substringWithRange
(or any other operation that creates an object of the right size) can stomp on it. – Hot Licks