在Objective-C检查布尔值的数组,看看是否至少有一个是YES [英] In Objective-C Check an array of Boolean Values and see if at least ONE is YES

查看:234
本文介绍了在Objective-C检查布尔值的数组,看看是否至少有一个是YES的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有布尔值的可变数组,我想检查,看看是否有任何值是YES。

I have a mutable array of Boolean values and I want to check to see if ANY of the values are YES.

在present我创建另一个阵列沿着这一块始终是全是假的,像这样;

At present I am creating another array alongside this one which is always ALL False like so;

 [MyArray addObject:[NSNumber numberWithBool:switchInput]];
 [MyAllNoArray addObject:[NSNumber numberWithBool:NO]];

用户做一些位和一些MYARRAY对象可能会变得YES,然后我用下面,看是否是真的还是假的。

The user does some bits and the some of the objects in MyArray may become YES, I then use the below to see if ANY are true or not.

if([MyArray isEqualToArray:MyAllNoArray])

我只是想知道是否有更好的方式(这种方式似乎浪费)?

I am just wondering if there is a better way (this way seems wasteful)?

我曾经想过一个计数器,每一次的对象之一更改计数器上升或下降(取决于更改为YES或NO),如果计数器为0,那么所以是数组。但我只是想有可能是我失踪IF语句中的一个更好的办法。

I have thought about a counter, each time one of the objects changes the counter goes up or down (depending on changing to YES or NO), if the counter is 0 then so is the array. But I am just thinking there may be a better way within an IF statement that I am missing.

推荐答案

我觉得一个简单的解决方案将是:

I think a simpler solution would be this:

NSArray *bools = @[@(NO),@(NO), @(YES)];
    if ([bools containsObject:@(YES)]) {
        // do your magic here
    }

循环和块将是一个矫枉过正,如果你只是需要检查presence。

Loops and blocks would be an overkill if you just need to check for presence.

这篇关于在Objective-C检查布尔值的数组,看看是否至少有一个是YES的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆