Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is that in each 'then' clause of the various <code>if</code> statements, you're creating a new local variable named <code>str</code>, assigning it to a new string, and then the variable goes out of scope. The compiler warning should tick you off to this: you're writing to a variable but never reading from it.</p> <p>Ordinarily, your code wouldn't compile, but you apparently have another variable named <code>str</code> in scope later on. Your new definitions of <code>str</code> are <em>shadowing</em> the old one: while the new name <code>str</code> is in scope, the name <code>str</code> refers to that variable, not the outer one, and the outer one is cannot be referred to.</p> <p>The solution is to move the declaration of <code>str</code> up to the top of the function. Furthermore, it's simpler just to use <code>[NSString stringWithFormat:@"blah"]</code> instead of <code>[[NSString alloc] initWithFormat:@"blah"]</code>, since the former gives you an autoreleased object. This saves you from having to manually <code>release</code> it later on. Note that assigning <code>lab.text=str</code> retains it, since the <code>text</code> property of the <code>UILabel</code> class has the <code>retain</code> modifier.</p> <pre><code>-(IBAction)buttonclick:(id)sender { NSString *title=[sender titleForState:UIControlStateNormal]; NSString *str; if([title isEqualToString:@"hello"]) { str=[NSString stringWithFormat:@"abc"]; } else if([title isEqualToString:@"nothing"]) { str=[NSString stringWithFormat:@"def"]; } else if([title isEqualToString:@"heaven"]) { str=[NSString stringWithFormat:@"ijk"]; } lab.text=str; } </code></pre> <p>Also note that with your original code, you had both a memory leak and memory corruption -- since you were allocating a string and then losing a reference to it (by the new local variable <code>str</code> going out of scope) without releasing it, and then you were calling <code>release</code> an extra time on whatever the outer <code>str</code> variable was. Moving the <code>str</code> declaration to the top of the function fixes both problems.</p> <p>I'm also assuming that your format strings are more complicated than just plain strings. If you're actually assigning constant strings such as <code>"abc"</code>, then of course it's much simpler to just do <code>str=@"abc"</code> instead of <code>str=[NSString stringWithFormat:@"abc"]</code>.</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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