开发者

Alter NSString to get nothing back

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.


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]).


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜