I’m just looking for a nicer and more efficient way to iterate through a given array of objects and compare a NSString property of each to another array just containing NSStrings.
My current code uses two for-each loops but it don’t think that it is the most efficient way.
for (MYClass *foo in arrayOfMyClass) {
for (NSString *ID in arrayOfStringIDs) {
if ([foo.Id isEqualToString:ID]) {
//Do something
break;
}
}
}
I think that it should be somehow possible to drop at least one loop with some cool tricks.
If all you want to know is if
foo.Idexists inarrayOfStringIDs, use an NSSet of strings instead. Then you can do:This avoids the second loop, since
containsObject:is generally much faster than O(n) for a set. You should, of course, do your own profiling as needed.