Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

variable was written, but never read?

Im getting the following warning variable 'isTaken' was written to, but never read on the following code :

func textFieldShouldEndEditing(textField: UITextField) -> Bool {

        var isTaken: Bool = false

        if textField == usernameTxt { var query = PFQuery(className: "_User")
        query = PFQuery(className: "_User")
        query.whereKey("username", equalTo: usernameTxt.text!)
        query.findObjectsInBackgroundWithBlock {
            (objects: [AnyObject]?, error: NSError?) in
            if error == nil {
                if (objects!.count > 0){

                    isTaken = true

                    }
                } else {
                    print("Username is available. ")
                }
            } else {
                print("error")
            }
          }
        }
        return true
    }

why am I getting the warning and how do I do away with it?

like image 541
Mike Strong Avatar asked Sep 17 '15 04:09

Mike Strong


3 Answers

As error says variable 'isTaken' was written to, but never read means you are creating isTaken instance and assigning a value to it but it never used.

like image 53
Dharmesh Kheni Avatar answered Oct 26 '22 06:10

Dharmesh Kheni


Just eliminate the statements:

var isTaken: Bool = false
isTaken = true

Since the value is never used, defining and assigning to it accomplishes nothing.

like image 7
zaph Avatar answered Oct 26 '22 06:10

zaph


Basically it's saying that isTaken is assigned a value, but it doesn't actually do anything in your code. You are never using it or checking it's value, so it's simply an warning saying that the variable is unnecessary.

If you actually are using isTaken and the compiler doesn't realize for some reason, you could probably just add another line right after

isTaken = true;

that just says

isTaken;

Or make isTaken global if you're using somewhere else in the code.

like image 4
mjmayank Avatar answered Oct 26 '22 07:10

mjmayank