0

Usually we write a function in our coding languages and have a return value, but some cases we don’t need returned variable in our code. when we neglect the returned value while calling the function, it shows a warning which is a little bit annoying. as you can see in the picture.

enter image description here

I know we can put _ instead of creating a variable but isn't there any proper method to remove this warning?

Community
  • 1
  • 1
Aqeel Ahmad
  • 709
  • 7
  • 20

2 Answers2

4

Add @discardableResult to your function.

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    neglectable()
}


@discardableResult func neglectable() -> String {
    return ""
  }
}
Pramod Shukla
  • 235
  • 2
  • 10
3

Add @discardableResult to your function and that will silence the warning.

@discardableResult func thisFunctionReturnsSomething(_ value1 : String) -> String { 
 return "blablabla"
}

now you can call it like

thisFunctionReturnsSomething("myString") // now it wont show the warning

If you don't want to use @discardableResult, you can try this too

_ = thisFunctionReturnsSomething("myString")
Keshu R.
  • 5,045
  • 1
  • 18
  • 38