Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>you need to release anything containing the word new, alloc/init or copy.</p> <p>also, you don't need to alloc/init this:</p> <pre><code>UIButton *placeHolderButton = [[UIButton alloc] init]; placeHolderButton = sender; </code></pre> <p>another way of doing this is:</p> <pre><code>UIButton *placeHolderButton = (UIButton *)sender; </code></pre> <p>in your version, it is allocating an instance with a retain count of +1, but you are immediately replacing the reference, so there is no way of releasing the memory later.</p> <p>you are creating a lot of instances with alloc/init, and then replacing their references with autoreleased instances.</p> <p>you could use</p> <pre><code> NSString *placeHolderString = [placeHolderString stringByAppendingString:firstValue.text]; </code></pre> <p>instead of</p> <pre><code> NSString *placeHolderString = [[NSString alloc] init]; placeHolderString = [placeHolderString stringByAppendingString:firstValue.text]; </code></pre> <p>which is again replacing a manually managed instance created on the first line, with an autoreleased instance on the second.</p> <p>infact you could replace every alloc/init in this with the factory method and not have to deal with memory at all in it as they would be autoreleased instances.</p> <pre><code>-(IBAction)inputNumbersFromButtons:(id)sender { //cast sender as a UIButton to suppress compiler warning, and allow us to reference it as placeholder button UIButton *placeHolderButton = (UIButton *) sender; int i = placeHolderButton.tag; NSString *addThisNumber = [NSString stringWithFormat:@"%i", i]; [firstValue setText:[firstValue.text stringByAppendingString:addThisNumber]]; } </code></pre> <p>If you look at the class docs for NSString, any method with a + next to it(ie +stringWithString:(NSString *)string) is a class method, don't use these methods on a reference after you have called alloc/init on it. </p>
 

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