ios - Objective-C 检查带整数的数组是否包含整数

标签 ios objective-c arrays int

<分区>

伙计们,我正在努力做到以下几点。 我有一个名为“specialLevels”的数组 (NSArray),该数组如下所示:

specialLevels = @[@2, @4, @6, @9];

这应该是一个整数数组。 我还得到了 int 'currentLevel'(基本 int 无对象)。

我想检查 currentLevel 是否在 specialLevels 数组中。 我知道方法“containsObject”存在,但这在我的情况下不起作用。

在这种情况下你们会建议做什么?

所以我是这么想的,但我觉得有点奇怪:

if ([specialLevels containsObject:[NSNumber numberWithInt:currentLevel]]) {
 // other code in here
}

最佳答案

你也可以这样写:

if ([specialLevels containsObject:@(currentLevel)]) {
    // other code in here
}

这更符合您其他代码的风格。

关于ios - Objective-C 检查带整数的数组是否包含整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19280618/

相关文章:

iphone - 使用批量请求在 Facebook 上传多张照片

c# - 生成数组并仅获取高于平均值的数字

ios - 在 iOS 上滚动时防止 TextInput 聚焦

cocoa-touch - 如何将 "upload"RAW 图像文件发送到 iOS 模拟器?

objective-c - 将 writeToFile :atomically: overwrite data?

javascript - 从边数组创建二维数组

c - 使用二维数组写访问冲突

ios - 需要在 iOS 应用程序上挥舞旗帜效果

ios - 三角测量 - 如何找到 3 个球体的交点(iBeacons)

ios - UILabel 的自动换行不起作用