views:

96

answers:

4

Hi there.

At two stages of my app's runtime, I am sending an NSString to the following method.

I receive no warnings or errors in the code editor or the debugger, but when I NSLog the output (secondString), I only get given the memory address of the object.

Is there something I am not doing right here?

- (NSString*)validateString
{
NSString *firstString = [NSString stringWithFormat:@"%@", self];
[firstString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSMutableString *secondString = [firstString mutableCopy];
[secondString replaceOccurrencesOfString:@"&" withString:@"%26" options:NSCaseInsensitiveSearch range:NSMakeRange([secondString length], 0)];

secondString = [NSString stringWithFormat:@"%@", secondString];
NSLog (@"%@ and %@", firstString, secondString);

return secondString;

[firstString release];
[secondString release];
}

I'd appreciate any help.

Thanks, Ricky.

+3  A: 

First some comments: 1) Everything after the return will not be executed, so the last to statements are useless (dead code). 2) If not created with +alloc, you can assumed that NSString instances are autoreleased, thus you do not need to send the -release message to firstString.

Edit: As Peter Hosey pointed out, you must however release the string obtained by -mutableCopy.

To answer your question: -stringByAddingPercentEscapesUsingEncoding: returns a pointer to the newly created instance, so you have to save it.

- (NSString*)validateString
{
NSString *firstString = [NSString stringWithFormat:@"%@", self];
firstString = [firstString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSMutableString *secondString = [firstString mutableCopy];
[secondString replaceOccurrencesOfString:@"&" withString:@"%26" options:NSCaseInsensitiveSearch range:NSMakeRange([secondString length], 0)];

 secondString = [NSString stringWithFormat:@"%@", secondString];
 NSLog (@"%@ and %@", firstString, secondString);

 [secondString release];

 return secondString;
 }
swegi
+2  A: 
  • You need to use %%26 if you want the string "%26"
  • your NSMakeRange is backwards
  • your return is too early, and you don't need to release the strings anyway
ergosys
And what @swegi said about stringByAddingPercentEscapesUsingEncoding
ergosys
The questioner does need to release the string obtained from `mutableCopy`.
Peter Hosey
@Peter, yes missed that.
ergosys
+3  A: 

Ugh that code is wrong on so many levels.

Here's a simpler version:

- (NSString*)validateString {
  NSString *firstString = [self stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  NSString *secondString = [firstString stringByReplacingOccurrencesOfString:@"&" withString:@"%%26" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [firstString length])];

  NSLog (@"%@ and %@", firstString, secondString);

  return secondString;
}

The construct [NSString stringWithFormat:@"%@", aString] is a pretty useless statement, especially when you have copy available. You also have a memory leak and a crash in your code (you create a copy of a string [+1 retain count], assign an autoreleased string into the same variable [+0 retain count, original string lost and leaked], and then release the autoreleased string [crash when the autorelease pool drains]).

Dave DeLong
A: 

Thanks to everyone for their help.

Dave, I used your code and it worked great.

Thanks again. Ricky.

Ricky