简体   繁体   中英

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;

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM