I have an array of objects, each containing a bool value with yes or no. I want to copy all objects with bool YES to another array. 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.
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.
Assuming your boolean array is called boolArray
, the code to get an array with only YES
would be something like this:
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.
EDIT: OK, let's assume that we have an object called MyDataObject
that has a bool property - 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.