[英]How do I look for bool values in array?
I have an array of objects, each containing a bool value with yes or no. 我有一个对象数组,每个对象包含一个带有yes或no的bool值。 I want to copy all objects with bool YES to another array.
我想将所有带有YES的对象复制到另一个数组。 How can i do that?
我怎样才能做到这一点? i have considered filtering the array using a predicate or integrating it in a for-loop, but i cant seem to get it right.
我曾考虑过使用谓词过滤数组或将其集成到for循环中,但我似乎无法正确处理。
I need something like this: 我需要这样的东西:
for (BOOL* opslag_Set in [dataSource allKeys]){
NSArray *array = [dataSource objectForKey:opslag_Set];
for (int j = 0; j < [array count]; j++) {
if ([[array objectAtIndex:j] isEqualToString:@"YES"]) {
add object to another array;
}
}
}
First object of my array: 我数组的第一个对象:
},
{
Dato = "2012-11-07 16:20:57 +0000";
Forfatter = "Vej 51, st. tv.";
Indhold = "Referat af beboerm\U00f8de";
"Opslag_set" = 0;
Overskrift = "Beboerm\U00f8de";
Prioritet = 0;
Svar = (
{
Dato = "2012-11-07 16:23:07 +0000";
Forfatter = "6. tv.";
Indhold = "Fedt fedt fedt";
}
);
},
You will have to use NSNumber
in order to store the bools in an array. 您必须使用
NSNumber
才能将布尔值存储在数组中。
Assuming your boolean array is called boolArray
, the code to get an array with only YES
would be something like this: 假设您的布尔数组称为
boolArray
,则仅使用YES
来获取数组的代码将如下所示:
NSMutableArray* temp = [NSMutableArray new];
for (NSNumber* value in boolArray)
{
if ([value boolValue])
{
[temp addObject:[NSNumber numberWithBool:YES]];
}
}
However, why are you trying to do this? 但是,为什么要尝试这样做? This will return an array with a certain number of elements that are all the same.
这将返回一个具有一定数量相同元素的数组。 You could just count the number if that's what you want.
如果需要的话,您可以只数一下。 The other thing I can think of is that you have an object with a bool property, in which case you can easily adapt the code above.
我能想到的另一件事是,您有一个带有bool属性的对象,在这种情况下,您可以轻松地修改上面的代码。
EDIT: OK, let's assume that we have an object called MyDataObject
that has a bool property - NSNumber* boolProperty
. 编辑:好的,我们假设我们有一个名为
MyDataObject
的对象,该对象具有bool属性MyDataObject
NSNumber* boolProperty
。 Here is the code: 这是代码:
NSMutableArray* temp = [NSMutableArray new];
for (MyDataObject* value in boolArray)
{
if ([value.boolProperty boolValue])
{
[temp addObject:value];
}
}
This should work for what you are doing. 这应该适合您的工作。
temp
will reference the original objects - they are not being copied. temp
将引用原始对象-不会复制它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.