0

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?

1
  • this is a common issue while using the method [array containsObject:x];, so its better to use the method shown in the answer below, but will consume more time as the contents of the array increases Commented Dec 7, 2012 at 9:45

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

7 Comments

No actually my array is NSMutableArray and I need to check some condition after getting BOOL value...I have edited my code ..Can u please have a look and rectify my mistake ..
@Sindhia i think you want to add the sunday value in another array right??
yes dude i know that sunday is string but i say that i think you want to add sunday if isThere Bool value return yes.. ok now 'use this dude.. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF IN %@", theArray]; BOOL result = [predicate evaluateWithObject:theString];
But I dont find NSLog get printed and I m gettting an orange coloured line below the code..It is neither showing error nor warning at that orange line
dude its work fine mate just take my code now and paste in your viewWillAppear method and see what u get its working fine dude.. try it now and reply in any error u got ok dude..
|
0

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"];

5 Comments

@Sindhia: It means you didn't get answer or didn't understand.
I didn't get answer .it is returning NO when a match occurs...what elz should I need to modify?
NSLog(@"your array:%@",yourArray); May i know what the output is?
( { Code=Sunday; }, { Code=Monday; }, { Code=Tuesday; } )
This is your getting array. But i have written code using this array. array = ( { Code=Sunday; }, { Code=Monday; }, { Code=Tuesday; } ). From this array, i derived another array(yourArray). The result: yourArray = (Sunday,Monday,Tuesday)
0
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;
 } 

}

Comments

0
NSString *yourString = @"Monday";

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

else
// Do the other Thing;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.