Objective c NSMutableArray的整布尔校验
在下面的代码中,我用布尔值填充了一个数组Objective c NSMutableArray的整布尔校验,objective-c,ios7,nsmutablearray,Objective C,Ios7,Nsmutablearray,在下面的代码中,我用布尔值填充了一个数组 for(int i = 0; i < 15; i++){ int checkVal = [(NSNumber *)[__diceValue objectAtIndex:i] intValue]; if(checkVal == matchVal){ [_diceMatch replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:y]]; } } for
for(int i = 0; i < 15; i++){
int checkVal = [(NSNumber *)[__diceValue objectAtIndex:i] intValue];
if(checkVal == matchVal){
[_diceMatch replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:y]];
}
}
for(int i=0;i<15;i++){
int checkVal=[(NSNumber*)[[uuu diceValue objectAtIndex:i]intValue];
if(checkVal==matchVal){
[\u diceMatch replaceObjectAtIndex:i with object:[NSNumber numberWithBool:y]];
}
}
写入条件以检查数组“\u diceMatch”中所有真值的最短方法是什么?最短方法?也许不是。最简单的方法?对
NSUInteger numberOfTrueValues = 0;
for (NSNumber *value in _diceMatch) {
if ([value boolValue]) {
numberOfTrueValues++;
}
}
- (BOOL)isDictMatchAllTrue {
for (NSNumber *n in _dictMatch) {
if (![n boolValue]) return NO;
}
return YES;
}
或者你不喜欢写循环
NSSet *set = [NSSet setWithArray:_diceMatch];
return set.count == 1 && [[set anyObject] boolValue];
注意:当数组为空时,第一个版本返回YES
,但第二个版本返回NO
你可以加上
if (_dictMatch.count == 0) return YES; //or NO
如果数组只能包含值“true”(
@YES
)或“false”(@NO
),则
然后,您只需检查是否缺少@NO
:
if (![_diceMatch containsObject:@NO]) {
// all elements are "true"
}