'>' 的左操作数是一个垃圾值
当我构建并放置我的应用程序时,我收到以下内存警告
“‘>’的左操作数是垃圾值”这一行 isTrue = (newLength > 20) ?否:是;
这里有什么问题。感谢您的帮助
(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger newLength;
BOOL isTrue;
if(textField == txtFirstName){
newLength = [textField.text length] + [string length] - range.length;
//isTrue = (newLength > 20) ? NO : YES;
}
if(textField == txtLastName){
newLength = [textField.text length] + [string length] - range.length;
//isTrue = (newLength > 20) ? NO : YES;
}
if(textField == txtEmail){
newLength = [textField.text length] + [string length] - range.length;
//isTrue = (newLength > 100) ? NO : YES;
}
if(textField == txtCompanyName){
newLength = [textField.text length] + [string length] - range.length;
//isTrue = (newLength > 30) ? NO : YES;
}
if(textField == txtPassword){
newLength = [textField.text length] + [string length] - range.length;
//isTrue = (newLength > 30) ? NO : YES;
}
if(textField == txtRe_EnterPassword){
newLength = [textField.text length] + [string length] - range.length;
//isTrue = (newLength > 30) ? NO : YES;
}
if(textField == txtZipCode){
newLength = [textField.text length] + [string length] - range.length;
//isTrue = (newLength > 20) ? NO : YES;
}
if(textField == txtCountry){
newLength = [textField.text length] + [string length] - range.length;
//isTrue = (newLength > 30) ? NO : YES;
}
isTrue = (newLength > 20) ? NO : YES;
return isTrue;
}
When i build and anylayse my app , i get the below memeory warning
"The left operand of '>' is a garbage value" at this line isTrue = (newLength > 20) ? NO : YES;
What is the problem over here.Thanks for any help
(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger newLength;
BOOL isTrue;
if(textField == txtFirstName){
newLength = [textField.text length] + [string length] - range.length;
//isTrue = (newLength > 20) ? NO : YES;
}
if(textField == txtLastName){
newLength = [textField.text length] + [string length] - range.length;
//isTrue = (newLength > 20) ? NO : YES;
}
if(textField == txtEmail){
newLength = [textField.text length] + [string length] - range.length;
//isTrue = (newLength > 100) ? NO : YES;
}
if(textField == txtCompanyName){
newLength = [textField.text length] + [string length] - range.length;
//isTrue = (newLength > 30) ? NO : YES;
}
if(textField == txtPassword){
newLength = [textField.text length] + [string length] - range.length;
//isTrue = (newLength > 30) ? NO : YES;
}
if(textField == txtRe_EnterPassword){
newLength = [textField.text length] + [string length] - range.length;
//isTrue = (newLength > 30) ? NO : YES;
}
if(textField == txtZipCode){
newLength = [textField.text length] + [string length] - range.length;
//isTrue = (newLength > 20) ? NO : YES;
}
if(textField == txtCountry){
newLength = [textField.text length] + [string length] - range.length;
//isTrue = (newLength > 30) ? NO : YES;
}
isTrue = (newLength > 20) ? NO : YES;
return isTrue;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当所有 if 条件均失败时,就会出现问题,即。如果
textField
不是您期望的集合之一。在这种情况下,
newLength
永远不会被分配,并且将是一个随机垃圾值。虽然这可能“永远不会发生”,但静态分析器并没有意识到这一点。
您应该将
newLength
初始化为默认值,也许为零。The problem occurs when all of the if conditions fail, ie. if
textField
is not one of the set you are expecting it to be.In this case,
newLength
is never assigned to, and will be a random garbage value.Whilst this is probably "never going to happen", the static analyzer is not aware of that.
You should initialize
newLength
to have a default value, perhaps zero.分析器无法确认您的“类似开关的构造”没有“失败”。如果所有这些if情况都失败,
newLength
的值可能永远不会被设置。那么在这种情况下,它就会被视为垃圾值。the analyzer cannot confirm that your 'switch-like construct' does not 'fall through'.
newLength
's value may never be set if all those if cases fail. then in that event, it would qualify as a garbage value.我对你的代码做了一些更改,我认为现在它会工作得更好。
I have made some changes to your code I think now it would be work better.
如果(至少)您的 if 测试之一始终保证通过,您的代码可能是正确的。但分析仪无法确定这一点。如果您能说服自己这一点,则可以忽略该警告。
但更好的想法是将它们改为 else if,最后放一个 else,其中包含一个 NSAssert。
Your code might be correct, if (at least) one of your if tests is always guaranteed to pass. But the analyzer cannot determine that. If you can convince yourself of that, you can ignore the warning.
But a better idea would be to change those into else ifs, and put one last else, inside which is an NSAssert.
尝试
如果 a < [a Compare:b] 返回 -1 b,如果 a == b 则为 0,如果 a > 则为 1 b.
isTrue = ([newLength比较:20]==1) ?否:是;
Try
[a compare:b] returns -1 if a < b, 0 if a == b and 1 if a > b.
isTrue = ([newLength compare:20]==1) ? NO : YES;