NSArray containsObject NSString Not Working?
I hava an array list (RatingsList) containing below contents
<CFArray 0x910e1a0 [0x2952380]>{type = mutable-small, count = 8, values = (
0 : <CFString 0x910e0d0 [0x2952380]>{contents = "NR-Adult"}
1 : <CFString 0x910e0f0 [0x2952380]>{contents = "NC-17/TV-MA"}
2 : <CFString 0x910e110 [0x2952380]>{contents = "R"}
开发者_如何学编程 3 : <CFString 0x910e120 [0x2952380]>{contents = "PG-13/TV-14"}
4 : <CFString 0x910e140 [0x2952380]>{contents = "PG/TV-PG"}
5 : <CFString 0x910e160 [0x2952380]>{contents = "G/TV-G"}
6 : <CFString 0x910e170 [0x2952380]>{contents = "TV-Y/TV-Y7"}
7 : <CFString 0x910e190 [0x2952380]>{contents = "NR"}
we are comparing for PG-13
[RatingsList containsObject:@"PG-13"]
return false.
Of course it returns false. Your array doesn't contain "PG-13", although it does contain "PG-13/TV-14".
Use a simple NSPredicate.
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains %@", aString];
NSArray *filteredArray = [RatingsList filteredArrayUsingPredicate:predicate];
if ([filteredArray count] > 0) return YES;
return NO;
精彩评论