Reputation: 411
I have a NSMutableArray and I need to compare the content in it with a NSString..But couldn't get it ?
My array
(
{
Code=Sunday;
},
{
Code=Monday;
},
{
Code=Tuesday;
}
)
I m comparing like this :
BOOL isTheObjectThere = [array containsObject:weekday];
if([arr count]==0 && isTheObjectThere==YES)
{
//do something
}
else
{
//do something
}
Here weekday is NSString whose value=Sunday
But isTheObjectThere is returning NO..
Where Im going wrong?
Upvotes: 0
Views: 1197
Reputation: 96
NSString *yourString = @"Monday";
for (NSString* item in inputArray){
if ([yourString rangeOfString:item].location != NSNotFound)
//Do something;
else
// Do the other Thing;
}
Upvotes: 0
Reputation: 20541
see this example
NSMutableArray* array = [NSArray arrayWithObjects:@"Sunday", @"Monday", @"Tuesday", nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF IN %@", array];
BOOL isTheObjectThere = [predicate evaluateWithObject:@"Sunday"];
NSLog(@"isTheObjectThere %d",isTheObjectThere);
its work fine and return isTheObjectThere 1
Upvotes: 1
Reputation: 7398
BOOL isTheObjectThere = [array containsObject:weekday];
will not work, better try the following method.
NSMutableArray* inputArray = [NSArray arrayWithObjects:@"Sunday", @"Monday", @"Tuesday", nil];
for (NSString* item in inputArray)
{
if ([item rangeOfString:@"Sunday"].location != NSNotFound)
{
//do something
}
else
{
//do something;
}
}
Upvotes: 0
Reputation: 6718
The array contains dictionary elements, then retrieve values of key(Code).
NSMutableArray *yourArray = [[NSMutableArray alloc] init];
for (NSDictionary *dict in array) {
[yourArray addObject:[dict valueForKey:@"Code"]];
}
BOOL isTheObjectThere = [yourArray containsObject:@"Sunday"];
Upvotes: 0