views:

102

answers:

2

Hi everyone, I am trying to find if there is any object in the mutable array that matches with the object I pass. Please see the function below.

The name comes from table view controller, so the row that is tapped is saved into name and passed to this function. I am trying to check whether the name in the table view controller equals to the name coming from the database. Here name refers to firstName and lastName..That's the reason I am appending those strings.

Please help me..I am confused of how to do this..if u have any better approach please let me know..thanks a lot..I think its trying to compare the memory locations of the object (not sure though) but is there any way so that it compares exactly the name that I am passing..

-(NSMutableDictionary *)getSearchContacts:(NSString *)name
{
 //---retrieve rows--- 
 NSString *qsql =[[NSString stringWithFormat:@"SELECT * FROM CONTACTS WHERE last_name LIKE '%@",[name substringToIndex:1]]stringByAppendingString:@"%' GROUP BY sugar_id ORDER BY last_name"]; 
 NSString *sugar_id;
 NSString *first_name;
 NSString *last_name;
 NSMutableArray *searchContacts=[[NSMutableArray alloc] init];

 sqlite3_stmt *statement;
 if (searchContactName == nil) {
  searchContactName=[[NSString alloc]init];
 }
 if (sqlite3_prepare_v2( db, [qsql UTF8String], -1, &statement, nil) == SQLITE_OK) {
  while (sqlite3_step(statement) == SQLITE_ROW) { 
   sugar_id= [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
   first_name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 3)];
   last_name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 4)];  

   if ([first_name isEqualToString:@"(null)"]) {
    last_name=[last_name stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[last_name substringToIndex:1] uppercaseString]];
    searchContactName=[searchContactName stringByAppendingString:last_name];
    searchContactName=[searchContactName stringByAppendingString:@":"];
    searchContactName=[searchContactName stringByAppendingString:@","];

   }
   else {

    first_name=[first_name stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[first_name substringToIndex:1] uppercaseString]];
    last_name=[last_name stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[last_name substringToIndex:1] uppercaseString]];
    searchContactName=[searchContactName stringByAppendingString:last_name];
    searchContactName=[searchContactName stringByAppendingString:@":"];
    searchContactName=[searchContactName stringByAppendingString:first_name];
    searchContactName=[searchContactName stringByAppendingString:@","];

   }
   [searchContacts addObject:searchContactName];
   //first_name = nil;
   //last_name = nil;
    if ([searchContacts containsObject:name]==YES) {
               searchSugarId=[[NSMutableDictionary alloc]initWithObjectsAndKeys:sugar_id,searchContactName,nil];
   }

  }
  //---deletes the compiled statement from memory--- 
  sqlite3_finalize(statement);
 } 
 return searchSugarId;

}

EDIT

Name: Blackmon:Valentin

Search Contact Name Barefield:Collin,Baylis:Efren,Beatty:Peter,Beckwith:Lynn,Benny:Wilma,Bermudes:Lenore,Berryhill:Gerard,Biles:Jodi,Blackmon:Valentin,Blassingame:Rose,Blume:Renae,Bonet:Claude,Bostic:Valerie,Bouldin:Renaldo,Bracewell:Brendan,Bradford:Kris,Brathwaite:Bill,Brugger:Ismael,Brumit:Julie,Buchholtz:Mathew,Bunker:Chrystal,Burch:Floyd,Burman:Sang,Butcher:Prince,Butcher:Rory,

searchContacts Barefield:Collin,Baylis:Efren,Beatty:Peter,Beckwith:Lynn,Benny:Wilma,Bermudes:Lenore,Berryhill:Gerard,Biles:Jodi,Blackmon:Valentin,Blassingame:Rose,Blume:Renae,Bonet:Claude,Bostic:Valerie,Bouldin:Renaldo,Bracewell:Brendan,Bradford:Kris,Brathwaite:Bill,Brugger:Ismael,Brumit:Julie,Buchholtz:Mathew,Bunker:Chrystal,Burch:Floyd,Burman:Sang,Butcher:Prince,Butcher:Rory,

A: 

Once you have all your data inside your array (therefore outside of the while), you need to iterate over the array and use the method isEqualToString for comparing name with the current array item.

Ok, so have a look at the code below which works:

+ (void)testContact {
NSArray *searchContacts = [NSArray arrayWithObjects:
                                                        @"Barefield:Collin,",
                                                        @"Baylis:Efren,",
                                                        @"Beatty:Peter,",
                                                        @"Beckwith:Lynn,",
                                                        @"Benny:Wilma,",
                                                        @"Bermudes:Lenore,",
                                                        @"Berryhill:Gerard,",
                                                        @"Biles:Jodi,",
                                                        @"Blackmon:Valentin,",
                                                        @"Blassingame:Rose,",
                                                        @"Blume:Renae,",
                                                        @"Bonet:Claude,",
                                                        @"Bostic:Valerie,",
                                                        @"Bouldin:Renaldo,",
                                                        @"Bracewell:Brendan,",
                                                        @"Bradford:Kris,",
                                                        @"Brathwaite:Bill,",
                                                        @"Brugger:Ismael,",
                                                        @"Brumit:Julie,",
                                                        @"Buchholtz:Mathew,",
                                                        @"Bunker:Chrystal,",
                                                        @"Burch:Floyd,",
                                                        @"Burman:Sang,",
                                                        @"Butcher:Prince,",
                                                        @"Butcher:Rory,",nil];
NSString *myContact = @"Blackmon:Valentin,";
for (NSString *contact in searchContacts) {
    if ([contact isEqualToString:myContact]) {
        NSLog(@"FOUND!!!!!");
        break;
    }
}
}

If you look carefully, you see that myContact is "Blackmon:Valentin," which has a comma at the end. In your array, all your elements have a comma at the end. But based on what you're showing in your post, the name you're looking for, i.e. Name: Blackmon:Valentin doesn't have a comma at the end. Maybe that's why you can't find it. Make sure you add the comma at the end and it should work.

Stelian Iancu
I tried to iterate over the values in the array but it's not entering the loop even if the values are equal.. I can't understand why :( This is what I am doing..for (int i=0; i<[searchContacts count]; i++) {if ([[searchContacts objectAtIndex:i] isEqualToString:name]) {searchSugarId=[[NSMutableDictionary alloc]initWithObjectsAndKeys:sugar_id,name,nil]; } }
racharambola
Don't know what to say other than: are you 100% sure the values are equal? As I said above, I tried to code I pasted here in Xcode and it worked for me.
Stelian Iancu
+1  A: 

containsObject will only return a boolean. You maybe want indexOfObject: method.

NSInteger index = [array indexForObject:anObject];

One thing I noticed in your code is that your are appending too many times a string into the same string. for example why not you use

searchContactName = [searchContactName stringByAppendingFormat:@"%@:%@,", last_name, first_name];

instead of

searchContactName=[searchContactName stringByAppendingString:last_name];
    searchContactName=[searchContactName stringByAppendingString:@":"];
    searchContactName=[searchContactName stringByAppendingString:first_name];
    searchContactName=[searchContactName stringByAppendingString:@","];

And more important: When doing searchContactName=[searchContactName stringByDoingSomething] make sure searchContactName is released otherwise you will be leaking memory.

for example here:

searchContactName=[[NSString alloc]init];
...
searchContactName=[searchContactName stringByAppendingString:last_name];

you are leaking the first object, then you are just making a new (autoreleased) object by appending last_name. If your are initializing like this: searchContactName=[searchContactName stringByAppendingString:last_name]; then is not necessary to do [[NSString alloc] init];

;) hope it helps

nacho4d
thanks for the reply..I will make the changes suggested by you..they were very helpful..hope I will figure out my problem..
racharambola
If the answer was helpful consider marking it as answer, your accept rate is pretty low
Leg10n