ios - 对 NSInteger 使用多个 else if 语句

标签 ios if-statement nsinteger

我有一段代码,其中我想要一个不同的 accessoryView 用于 TableView 单元格,基于该单元格条目的编号。我设置的代码是:

NSInteger warriors = [entry.prayerWarriors intValue];

        if (warriors == 0) {
            //Do nothing
            //NSLog(@"0");
        }
        else if (0 < warriors < 50) {
            cell.accessoryView = firstLevel;
           // NSLog(@"50");

        }
        else if (51 < warriors < 100) {
            cell.accessoryView = secondLevel;
          //  NSLog(@"100");

        }
        else if (101 < warriors < 500) {
            cell.accessoryView = thirdLevel;
         //   NSLog(@"500");

        }
        else {
            cell.accessoryView = fourthLevel;
         //   NSLog(@"A Lot");

        }

但是,它始终只返回 warriors == 0 的第一个条目。我做错了什么?

最佳答案

与其这样做...

else if (0 < warriors < 50) {
    cell.accessoryView = firstLevel;
    // NSLog(@"50");
}

这样做...

else if (0 < warriors && warriors < 50) {
    cell.accessoryView = firstLevel;
    // NSLog(@"50");
}

编辑

要回答您的评论...您可能是想在其中添加一些 <= 或 >=,因为当 warriors 等于您的 if 条件(50、100 或 500)的边界时,它将转到最后一个 else。

你可能希望它看起来像这样......

 NSInteger warriors = [entry.prayerWarriors intValue];

    if (warriors == 0) {
        //Do nothing
        //NSLog(@"0");
    }
    else if (0 < warriors && warriors <= 50) {
        cell.accessoryView = firstLevel;
        // NSLog(@"50");

    }
    else if (50 < warriors && warriors <= 100) {
        cell.accessoryView = secondLevel;
        //  NSLog(@"100");

    }
    else if (100 < warriors && warriors <= 500) {
        cell.accessoryView = thirdLevel;
        //   NSLog(@"500");

    }
    else {
        cell.accessoryView = fourthLevel;
        //   NSLog(@"A Lot");

    }

关于ios - 对 NSInteger 使用多个 else if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23551765/

相关文章:

objective-c - 将 NSInteger 转换为字节

objective-c - 将 NSString 转换为 Integer 并检查它是否小于或等于某个值

ios - 在 NSMutableArray 中保留 UIBezierPaths

objective-c - 调试 iOS 应用程序时如何在 Xcode 控制台中打印 unsigned char?

mysql - MySQL触发器中的IF语句

r - 使用 if 语句对数字进行分类的循环

java - 搜索数组列表对的集合

iOS : NSInteger and NSUInteger comparison

ios - uitableview如何动态解析数据多单元格

ios - 何时使对象为零以及何时调用释放