我有一个NSMutableArray,我需要将其中的内容与NSString进行比较..但是无法得到它?
我的阵列
(
{
Code=Sunday;
},
{
Code=Monday;
},
{
Code=Tuesday;
}
)
我这样比较:
BOOL isTheObjectThere = [array containsObject:weekday];
if([arr count]==0 && isTheObjectThere==YES)
{
//do something
}
else
{
//do something
}
这里的工作日是NSString,其值=星期日
但isTheObjectThere返回NO ..
哪里出错了?
答案 0 :(得分:1)
见这个例子
NSMutableArray* array = [NSArray arrayWithObjects:@"Sunday", @"Monday", @"Tuesday", nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF IN %@", array];
BOOL isTheObjectThere = [predicate evaluateWithObject:@"Sunday"];
NSLog(@"isTheObjectThere %d",isTheObjectThere);
工作正常并返回 isTheObjectThere 1
答案 1 :(得分:0)
数组包含字典元素,然后检索键(代码)的值。
NSMutableArray *yourArray = [[NSMutableArray alloc] init];
for (NSDictionary *dict in array) {
[yourArray addObject:[dict valueForKey:@"Code"]];
}
BOOL isTheObjectThere = [yourArray containsObject:@"Sunday"];
答案 2 :(得分:0)
BOOL isTheObjectThere = [array containsObject:weekday];
无效,请尝试以下方法。
NSMutableArray* inputArray = [NSArray arrayWithObjects:@"Sunday", @"Monday", @"Tuesday", nil];
for (NSString* item in inputArray)
{
if ([item rangeOfString:@"Sunday"].location != NSNotFound)
{
//do something
}
else
{
//do something;
}
}
答案 3 :(得分:0)
NSString *yourString = @"Monday";
for (NSString* item in inputArray){
if ([yourString rangeOfString:item].location != NSNotFound)
//Do something;
else
// Do the other Thing;
}