I have an 3 NSMutableArray
objects that contain CMTime
objects. How can I iterate through all three of them in an efficient manner and find out if there are duplicate values in all three? For example, I'm iterating through one of time and reading the value and storing it in x
. Now, I want to see if x
occurs (at any position) within the other two arrays. I tried looking for a contains
method, but couldn't find one. I did come across filterUsingPredicate
, but开发者_JAVA百科 I'm not sure if this is the best way of doing it nor how to actually use predicates.
I tried looking for a contains method, but couldn't find one.
Use indexOfObject:
like this:
if ([array indexOfObject:object] != NSNotFound) {
// object found
}
else {
// object not found
}
You can use ([yourArray indexOfObject:x] != NSNotFound)
in place of your missing contains
method. However, if you're doing this quickly, often, or with a lot of elements, you should consider using NSMutableOrderedSet
, which is ordered like NSMutableArray
, but offers a quick and efficient contains
method, as well as allowing quick operations like union and intersection, which might allow you to redesign your algorithm to iterate through your elements much less.
精彩评论