0

我有一个 NSMutableArray,我需要将其中的内容与 NSString 进行比较。但无法得到它?

我的阵列

(
{
  Code=Sunday;
},
{
 Code=Monday;
},
{
Code=Tuesday;
}
)

我是这样比较的:

BOOL isTheObjectThere = [array containsObject:weekday];

if([arr count]==0 && isTheObjectThere==YES)
{
//do something
}
else
{
//do something
}

这里 weekday 是 NSString ,其 value=Sunday

但是 isTheObjectThere 正在返回 NO..

我哪里错了?

4

4 回答 4

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);

它的工作正常并返回是TheObjectThere 1

于 2012-12-07T09:42:32.867 回答
0

该数组包含字典元素,然后检索键(代码)的值。

 NSMutableArray *yourArray = [[NSMutableArray alloc] init];
    for (NSDictionary *dict in array) {
        [yourArray addObject:[dict valueForKey:@"Code"]];
    }
    BOOL isTheObjectThere = [yourArray containsObject:@"Sunday"];
于 2012-12-07T09:43:18.630 回答
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;
 } 

}
于 2012-12-07T09:51:27.580 回答
0
NSString *yourString = @"Monday";

for (NSString* item in inputArray){
if ([yourString rangeOfString:item].location != NSNotFound)
//Do something;

else
// Do the other Thing;
}
于 2012-12-07T11:53:49.067 回答