Xcode 中的“Dead Store”警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7597679/
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
"Dead Store" Warning in Xcode
提问by learner2010
I am getting a dead store warning when I analyze my project but the project does not crash. Here is what I am doing
当我分析我的项目但项目没有崩溃时,我收到了一个死店警告。这是我在做什么
NSString *graphUrl = nil;
if ([graphArray count] == 1)
{
objTrial = [graphArray objectAtIndex:0];
graphUrl = @"http://chart.apis.google.com/chart?cht=s:nda&chf=bg,s,FFFFFF&chs=";
graphUrl = [graphUrl stringByAppendingString:@"&chd=t:"];
graphUrl = [graphUrl stringByAppendingString:objTrial.highValue];// get the dead store error here
}
else
{
//someother operation is done and a value is loaded to aURL
}
I get a dead store warning as mentioned in the code.. How can I prevent this?
我收到代码中提到的死店警告..我该如何防止?
It would be great if someone could help me out in this
如果有人能帮我解决这个问题,那就太好了
回答by Circumflex
The warning is telling you that the store that you do in the first line gets thrown away (i.e assigning an empty string to the variable and then reassigning it afterwards without using the original value). Just change the first line to the following and the warning should go away:
警告告诉您,您在第一行中执行的存储会被丢弃(即,将空字符串分配给变量,然后在不使用原始值的情况下重新分配它)。只需将第一行更改为以下内容,警告就会消失:
NSString *aUrl;
Edit:
编辑:
you should change the line where you use it also:
您还应该更改使用它的行:
aURL = [aValue copy];
回答by EmptyStack
Dead Storeis a value that is assigned but never used. There is nothing to worry about it. But if you can't control yourself from worrying ;-) you can change your code to,
Dead Store是一个已分配但从未使用过的值。没有什么可担心的。但是,如果您无法控制自己的担心;-) 您可以将代码更改为,
NSString aUrl = nil;
if ([anArray count] == 1) {
// a value is store in aValue
// then that value is appended to aURL
aURL = [aURL stringByAppendingString:aValue];
} else {
aUrl = @"";
//someother operation is done and a value is loaded to aURL
}
回答by Max MacLeod
"dead store" means something that's not used, or rather something useless.
“dead store”是指没有使用的东西,或者更确切地说是无用的东西。
You get it when you have a variable defined that you never do anything with. So, the Analyzer tells you that you have wasted some storage.
当你定义了一个你从来没有做任何事情的变量时,你就会得到它。因此,Analyzer 会告诉您浪费了一些存储空间。
Here you haven't used the aUrl object after assigning it.
这里你还没有使用分配后的 aUrl 对象。
It won't cause any problems other than a few bytes of wasted memory. Of course if it's a large object that could be more.
除了浪费几个字节的内存之外,它不会引起任何问题。当然,如果它是一个大对象,可能会更多。
Perhaps someone could chip in with knowledge of compilers, as compiler optimization might take care of dead stores in any case.
也许有人可以了解编译器的知识,因为编译器优化在任何情况下都可能会处理死存储。