0

I solved the problem by following the links that appear when you start a question
they told me how, but not why...
so my question is to understand compiler warning language

In Xcode
I wanted a button that triggers an AlertView to assume the result of what the user enters therein:

UITextField *textfield = [alertView textFieldAtIndex:0];
circumference = [textfield.text floatValue];
NSString *myString = textfield.text;
[_myButton setTitle:(@"%@",myString) forState:UIControlStateNormal];

Well, it works, plus a warning.
I should pose my question here so: the warning said "Expression Result Unused". How do they figure that? I was using the result of the expression - right there on the button.

for those concerned, this is the fix:

UITextField *textfield = [alertView textFieldAtIndex:0];
circumference = [textfield.text floatValue];
[_myButton setTitle:[NSString stringWithFormat:@"%@",textfield.text] forState:UIControlStateNormal];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
aremvee
  • 179
  • 1
  • 13
  • The *expression* `(@"%@",myString)` evaluates to `myString` (via the "comma operator") and thus the result of the first expression (`@"%@"`) is discarded/unused. – user2864740 Nov 04 '13 at 07:20
  • Do you use the var "myString" ,"circumference" anywhere else? If not, just remove it. – Ramaraj T Nov 04 '13 at 07:21
  • 1
    http://stackoverflow.com/a/1618867/2864740 , http://stackoverflow.com/a/8620506/2864740 – user2864740 Nov 04 '13 at 07:21
  • @user2864740 - thanks for the links, interesting reading. – aremvee Nov 04 '13 at 07:42
  • after reading the links, ( a fair bit is still over my head ) it seems using the comma as an operator sets up a little parameterStack from which the last entry is dropped when read. Hmmm, i can think of a use for it already – aremvee Nov 04 '13 at 08:12
  • Your "fix" is incorrect (well technically it's correct, it's just pointless). Don't use `stringWithFormat` unless you actually need to format something. In this case you can directly use `textfield.text`. Get rid of the `stringWithFormat`. – rmaddy Nov 04 '13 at 15:13

2 Answers2

1
[_myButton setTitle:(@"%@",myString) forState:UIControlStateNormal];

makes little sense. I would actually expect a syntax error, but due to the comma operator

(@"%@",myString)

is actually like writing

({
    @"%@";
    myString;
})

so you wrote the equivalent of

[_myButton setTitle:({
    @"%@";
    myString;
}) forState:UIControlStateNormal];

(And in case anyone is wondering, yes, this is valid syntax as well.)

So ultimately (@"%@",myString) evaluates to myString, but the expression @"%@" results unused and you get a warning.

setTitle:forState: expects a NSString as first parameter and you happen to have already have a NSString, named myString.

Just do

[_myButton setTitle:myString forState:UIControlStateNormal];
Community
  • 1
  • 1
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
  • Certain syntax choices (of just about any language) always surprise me xD – user2864740 Nov 04 '13 at 07:24
  • Good Explanation, and your variant works too, i just tried it and that's what i'll use. I'm not seasoned enough to break free from the Log-to-Console mentality. – aremvee Nov 04 '13 at 07:38
  • By the way, you should never ever do `[NSString stringWithFormat:@"%@", aString]`. It's completely useless and you are just wasting memory. – Gabriele Petronella Nov 04 '13 at 07:40
0

You are receiving the value for the textfield in the variable myString but you are not using it anywhere else. That's why you got that warning "Expression Result Unused". Just remove that line.

UITextField *textfield = [alertView textFieldAtIndex:0];
circumference = [textfield.text floatValue];
//NSString *myString = textfield.text;
[_myButton setTitle:textfield.text forState:UIControlStateNormal];
Ramaraj T
  • 5,184
  • 4
  • 35
  • 68
  • Actually that line was commented out when i collected it from Xcode, I used it that way up to the point of asking the question. I grabbed the whole shebangus of some variations i tried meaning to arrange it after pasting. I forgot. In the time it took to edit the question here, you slipped your comment in. – aremvee Nov 04 '13 at 07:28
  • And in any case it would be a different warning. It would say something about the variable being unused, as opposed to the expression. – Gabriele Petronella Nov 04 '13 at 07:33
  • @aremvee this answer is wrong in any case. As I said, an unused variable and an unused expression generate two different warnings. The issue was cause by that weird construct you inadvertently used to pass the string as a parameter. – Gabriele Petronella Nov 04 '13 at 07:45