Reputation: 179
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];
Upvotes: 0
Views: 6129
Reputation: 108121
[_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];
Upvotes: 1
Reputation: 5230
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];
Upvotes: 0