Sindhia
Sindhia

Reputation: 411

compare a string with NSMutablearray data in xcode?

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

Answers (4)

polterfest
polterfest

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

Paras Joshi
Paras Joshi

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

DD_
DD_

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

Prasad G
Prasad G

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

Related Questions