Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

swift2

In several cases of my old code I am getting the warning:

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

For instance this often occurs if I am checking an enum which has an associated object (but I don't use the object in this case) - or if I am using the 'If let' construct to check if an object is nil or not.

It doesn't bother me that much to go through these warnings and make the changes, but I wondered if anyone knew why this is recommended - why bother warning us about this? Would it affect performance in any way?

like image 248
James Alvarez Avatar asked Jun 23 '15 12:06

James Alvarez


3 Answers

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.

like image 112
Qbyte Avatar answered Nov 19 '22 16:11

Qbyte


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();
like image 38
Abhimanyu Rathore Avatar answered Nov 19 '22 15:11

Abhimanyu Rathore


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) <---
like image 2
fred Avatar answered Nov 19 '22 15:11

fred