Ignoring the return value of the function declared with the warn_unused_result attribute

We becomeFirstResponder this warning in all calls to the becomeFirstResponder function in our Objective-C code.

Ignoring the return value of the function declared with the warn_unused_result attribute

 [self.phoneNumberField becomeFirstResponder]; // warning here!!! 

What is the best way to suppress this warning here?


Edit: I also tried this,

 BOOL ignore = [self.emailField becomeFirstResponder]; 

But at the same time Xcode warns about ignore variable is not used: (


I also tried this,

 BOOL ignore = [self.phoneNumberField becomeFirstResponder]; if (ignore) { } 

The warnings in this case have disappeared. But I don’t think I can even go through my own code review. This is too ugly!

+5
source share
1 answer

This should work to pass the expression to the void expression.

 (void)[self.emailField becomeFirstResponder]; 

(Even I cannot reproduce the warning. However, this may depend on warning flags.)

For your editing:

 BOOL ignore = [self.emailField becomeFirstResponder]; 

Probably

 BOOL ignore = [self.emailField becomeFirstResponder]; ignore = ignore; // or (void)ignore; 

the warning should be removed. (He does with my flags.) However, this is an ugly hack.

By the way: There is a reason for the attribute. Perhaps you should double-check if this is good, and not check the return value.

+19
source

Source: https://habr.com/ru/post/1266375/


All Articles