Note that there are some explanatory texts on larger screens.

plurals
  1. POString manipulation without memory leaks?
    primarykey
    data
    text
    <p>I'd like to do a series of string substitutions to removed xml-escaped chars such as <code>'&amp;amp;'</code>. </p> <p>1) Is there an existing UIKit function that can do this?</p> <p>2) If not, what's the best way to do it without leaking memory? Here's the idea:</p> <pre><code>-(NSString*) unescape:(NSString*)string { string = [string stringByReplacingOccurrencesOfString:@"&amp;apos;" withString:@"'"]; string = [string stringByReplacingOccurrencesOfString:@"&amp;amp;" withString:@"&amp;"]; string = [string stringByReplacingOccurrencesOfString:@"&amp;quot;" withString:@"\""]; string = [string stringByReplacingOccurrencesOfString:@"&amp;gt;" withString:@"&gt;"]; string = [string stringByReplacingOccurrencesOfString:@"&amp;lt;" withString:@"&lt;"]; return string; } </code></pre> <p>But doesn't that leak memory with each assignment? Or does stringByReplacingOccurrencesOfString return autoreleased strings? How do we confirm that stringByReplacingOccurrencesOfString strings are autoreleased? Or should we wrap them with <code>[... autorelease]</code>?</p> <p>Even if they are autoreleased, it's preferable to avoid autorelease on the iPhone. (See <a href="http://kosmaczewski.net/2009/01/28/10-iphone-memory-management-tips/" rel="nofollow noreferrer">here</a>). So then we would do:</p> <pre><code>-(NSString*) unescape:(NSString*)string { NSString* string2 = [string stringByReplacingOccurrencesOfString:@"&amp;apos;" withString:@"'"]; // don't release 'string' because we didn't allocate or retain it NSString* string3 = [string2 stringByReplacingOccurrencesOfString:@"&amp;apos;" withString:@"'"]; [string2 release]; NSString* string4 = [string3 stringByReplacingOccurrencesOfString:@"&amp;apos;" withString:@"'"]; [string3 release]; //...and so on } </code></pre> <p>But that's pretty ugly code. What's the best way to write this code to do multiple substitutions? How would you do it?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload