'>' 的左操作数是一个垃圾值

发布于 2024-12-12 16:34:08 字数 1775 浏览 0 评论 0原文

当我构建并放置我的应用程序时,我收到以下内存警告

“‘>’的左操作数是垃圾值”这一行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

月棠 2024-12-19 16:34:08

当所有 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.

樱桃奶球 2024-12-19 16:34:08

分析器无法确认您的“类似开关的构造”没有“失败”。如果所有这些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.

撞了怀 2024-12-19 16:34:08

我对你的代码做了一些更改,我认为现在它会工作得更好。

(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSUInteger newLength = 0;
    BOOL isTrue = false;

    if(textField == txtFirstName){
        newLength = [textField.text length] + [string length] - range.length;
        isTrue = (newLength > 20) ? NO : YES;
    }
    else if(textField == txtLastName){
        newLength = [textField.text length] + [string length] - range.length;
        isTrue = (newLength > 20) ? NO : YES;
    }
    else if(textField == txtEmail){
        newLength = [textField.text length] + [string length] - range.length;
        isTrue =  (newLength > 100) ? NO : YES;
    }
    else if(textField == txtCompanyName){
        newLength = [textField.text length] + [string length] - range.length;
        isTrue =  (newLength > 30) ? NO : YES;
    }
    else if(textField == txtPassword){
        newLength = [textField.text length] + [string length] - range.length;
        isTrue =  (newLength > 30) ? NO : YES;
    }
    else if(textField == txtRe_EnterPassword){
        newLength = [textField.text length] + [string length] - range.length;
        isTrue =  (newLength > 30) ? NO : YES;
    }
    else if(textField == txtZipCode){
        newLength = [textField.text length] + [string length] - range.length;
        isTrue =  (newLength > 20) ? NO : YES;
    }
    else if(textField == txtCountry){
        newLength = [textField.text length] + [string length] - range.length;
        isTrue =  (newLength > 30) ? NO : YES;
    }
    else {
        isTrue = (newLength > 20) ? NO : YES;
    }

    return isTrue;
}

I have made some changes to your code I think now it would be work better.

(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSUInteger newLength = 0;
    BOOL isTrue = false;

    if(textField == txtFirstName){
        newLength = [textField.text length] + [string length] - range.length;
        isTrue = (newLength > 20) ? NO : YES;
    }
    else if(textField == txtLastName){
        newLength = [textField.text length] + [string length] - range.length;
        isTrue = (newLength > 20) ? NO : YES;
    }
    else if(textField == txtEmail){
        newLength = [textField.text length] + [string length] - range.length;
        isTrue =  (newLength > 100) ? NO : YES;
    }
    else if(textField == txtCompanyName){
        newLength = [textField.text length] + [string length] - range.length;
        isTrue =  (newLength > 30) ? NO : YES;
    }
    else if(textField == txtPassword){
        newLength = [textField.text length] + [string length] - range.length;
        isTrue =  (newLength > 30) ? NO : YES;
    }
    else if(textField == txtRe_EnterPassword){
        newLength = [textField.text length] + [string length] - range.length;
        isTrue =  (newLength > 30) ? NO : YES;
    }
    else if(textField == txtZipCode){
        newLength = [textField.text length] + [string length] - range.length;
        isTrue =  (newLength > 20) ? NO : YES;
    }
    else if(textField == txtCountry){
        newLength = [textField.text length] + [string length] - range.length;
        isTrue =  (newLength > 30) ? NO : YES;
    }
    else {
        isTrue = (newLength > 20) ? NO : YES;
    }

    return isTrue;
}
沒落の蓅哖 2024-12-19 16:34:08

如果(至少)您的 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.

梦幻的味道 2024-12-19 16:34:08

尝试
如果 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;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文