Swift 2: Immutable value X is never used consider replacing with '_'

Generally you should work warning free in day-to-day work. The obvious reason is that you are going to miss the real and important warnings if your code produces hundreds of not so important warnings.

The case you are describing is best solved in this way: instead of using if let myObject = myObject you better use:

if myObject != nil 

In this way it is clear what you want to do.


It is because your value was never used after declaring it, I had the exact same problem not too long ago.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)


    let row = indexPath.row <-----

I found that just printing it after made the error go away.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)


    let row = indexPath.row <---
    print(row) <---

Immutable value X is never used consider replacing with '_'

this warning coming because you are not using your variable after declaring it. just ignore the warning and keep writing your code and use your object , it will vanished .

Swift 2 is very sensitive language it will give you warning on every step .

i am explaining a simple demo of warning of any object here :- 1 .Immutable value X is never used consider replacing with '_' it will come when you will do not use of object and declare it only. Ex:-

var nameImg:String = "";

2.variable 'nameImg'was written to,but never read. it will come when you will assign your object any value but you are still not using it in any other object . Ex-

  var nameImg:String = "";
                    if(action==1){
                        nameImg  = "navTab";
                    }
                    else{
                        nameImg  = "back";
                    }

3. No Warnings will come if you are assigning any value to your object and using it in another object too ;).

Ex-

 var nameImg:String = "";
                    if(action==1){
                        nameImg  = "navTab";
                    }
                    else{
                        nameImg  = "back";
                    }


                    //back & menu btn
                    let btnBack = UIButton(type: UIButtonType.Custom);
                    btnBack.setImage(UIImage(named:nameImg), forState: UIControlState.Normal);
                    btnBack.sizeToFit();

These changes are recommended because if you don't use these variables at all you should consider replacing it with '_'. So you (and other people) can later understand your intention more quickly.

In terms of performance it would probably be just a minor improvement which you can't notice at all. The same situation applies during compilation and optimization of your code.

Tags:

Swift2