xcode 变量已写入,但从未读取?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32622790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
variable was written, but never read?
提问by Mike Strong
Im getting the following warning variable 'isTaken' was written to, but never read
on the following code :
我variable 'isTaken' was written to, but never read
在以下代码中收到以下警告:
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?
为什么我会收到警告以及如何消除它?
回答by Dharmesh Kheni
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.
正如错误所说,variable 'isTaken' was written to, but never read
您正在创建isTaken
实例并为其分配一个值,但它从未使用过。
回答by zaph
Just eliminate the statements:
只需消除以下语句:
var isTaken: Bool = false
isTaken = true
Since the value is never used defining and assigning to if accomplishes nothing.
由于从未使用该值定义和分配给 if 什么也不做。
回答by mjmayank
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.
基本上它是说 isTaken 被分配了一个值,但它实际上并没有在你的代码中做任何事情。您永远不会使用它或检查它的值,因此它只是一个警告,说明该变量是不必要的。
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 并且编译器由于某种原因没有意识到,您可能只需在之后添加另一行
isTaken = true;
that just says
那只是说
isTaken;
Or make isTaken global if you're using somewhere else in the code.
或者,如果您在代码中的其他地方使用,则将 isTaken 设为全局。
回答by Abhinav
Its a compiler warning to point out a dead code. You probably have copy pasted some code and removed some unwanted code. In doing so, usage of local variable isTaken
is gone. So, its only being assigned a value and never used for materializing any benefits. You can either simply remove the code around isTaken
or double check and put back the functionality around it :).
它是一个编译器警告,指出一个死代码。您可能已经复制粘贴了一些代码并删除了一些不需要的代码。这样一来,局部变量的使用isTaken
就消失了。所以,它只是被分配了一个值,从不用于实现任何好处。您可以简单地删除周围的代码isTaken
或仔细检查并放回周围的功能:)。
回答by dede.exe
It's warning you about a var that you set a value, but don't operate over it after.
它警告您设置了一个值,但之后不要对其进行操作。
Is very important keep your code clean and safe, so the xcode just gives you a little help with it.
保持你的代码干净和安全非常重要,所以 xcode 只是给你一点帮助。
isTaken = true;
Thats the point you set a value to isTakenvariable.
这就是您为isTaken变量设置值的要点。
Try to review your code and think about the use of this variable.
尝试查看您的代码并考虑使用此变量。