简体   繁体   中英

How to get rid of warning “Result of call 'resignFirstResponder()' is unused”?

Since I converted my code to Swift 3 , I got a warning whenever I don't use the result of UITextField 's resignFirstResponder() method.

在此输入图像描述

There's no bugs ( at least for now ) but I don't really like to have warnings like that. So my questions:

  • Should I actually use the results of this kind of call ? And if the answer yes, what should I do with them ?
  • If the answer is no : how can I get rid of these warnings ?

As Shaggy D already said this warnings can be ignored safely.

If you don't want to see them at all in your project you may set GCC_WARN_UNUSED_VALUE to false/NO in Build Settings .

Or just add let _ = before resignFirstResponder() :

let _ = resignFirstResponder()

1) You can ignore the result safely

2) Put these lines before the call :

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-result"

resignFirstResponder will return false when the object you are asking to resign refuses to do so. For example, a text field will return false if it is in the middle of being edited, and I should imagine that the search bar in your example would do the same. If you ignore the result of these calls, therefore, you could get yourself into a bit of a mess.

Warnings in XCode can be annoying, but my personal view is that they are generally helpful and you should edit your code to get rid of them. When I'm working on a load of new code I tend to accrue warnings but always clean them down regularly.

In this situation I would advise you add some code to cover the eventuality of the search bar refusing to relinquish first responder status.

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM