0

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?

user1007895
  • 3,925
  • 11
  • 41
  • 63
  • I suspect that `dateString` is not retained, and when you create `temp` it stomps on `dateString`. (You should show the logic that creates `dateString`.) – Hot Licks Dec 16 '11 at 20:10
  • 1
    `substringWithRange:` won't alter the source (`dataString`) object, so it won't "stomp". My guess is `dataString` is not actually mutable (`NSMutableString`). – gschandler Dec 16 '11 at 20:18
  • @HotLicks if `dateString` was not retained (-> released) the code would crash on `dateString.length` in line 1. I suspect you referred to an autoreleased `dateString`, 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. – Ahti Dec 16 '11 at 20:18
  • @Ahti -- Nope, the code wouldn't crash until another object is allocated over the top of `dateString`. But that other object is being allocated when `temp` is created. Have seen this sort of thing on several occasions. – Hot Licks Dec 16 '11 at 20:25
  • @gschandler -- If `dateString` 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 Dec 16 '11 at 20:26
  • You could enable NSZombies. If you have XCode4 check this [link](http://stackoverflow.com/questions/2190227/how-do-i-set-nszombieenabled-in-xcode-4). If you have XCode 3 check this other [link](http://stackoverflow.com/questions/5386160/how-to-enable-nszombie-in-xcode). In this manner you can find if an object has been deallocated. Hope it helps. – Lorenzo B Dec 16 '11 at 20:28
  • user1007895 -- If you don't believe I'm right, try hardcoding `dateString = @"2011-10-07T08:55:16-05:00";` right ahead of your `NSMakeRange` statement, and see if that doesn't prevent it from crashing. [Edited: Well, I guess you'd have to cast it to a mutable string as well.] – Hot Licks Dec 16 '11 at 20:31
  • @Hot Licks, yes in that case it would feasible. Although I would suspect that `substringWithRange:` would throw an exception first. – gschandler Dec 16 '11 at 20:32
  • What's the crash? Don't assume it's a memory crash. This might be an out-of-bounds exception if range is {NSNotFound,0}. – Rob Napier Dec 16 '11 at 20:46
  • @RobNapier -- "Crashes with no errors" is generally bad memory reference. – Hot Licks Dec 16 '11 at 21:47

1 Answers1

0

try this :

NSMutableString *dateString = [NSMutableString stringWithString:@"2011-10-07T08:55:16-05:00"];

NSRange range = NSMakeRange(dateString.length-3, 1);
NSString *temp = [dateString substringWithRange:range];
if ([temp isEqualToString:@":"])
    [dateString replaceCharactersInRange:range withString:@""];
Maulik
  • 19,348
  • 14
  • 82
  • 137
  • That will replace all three `:` characters, but he only wants to do the last one. – Hot Licks Dec 16 '11 at 20:28
  • Which is identical to the OP's. Which means that there is something wrong with `dateString`, since there are no other options. – Hot Licks Dec 16 '11 at 21:49