views:

81

answers:

2

Hi,

In my tableView at the numberOfRowInSection, I try to compare myDate inside self.date to dateInFiche inside self.allDates.

my date is like: 12-05-1986
in the For statement dateinFiche will have those value:
12-05-1986
12-05-1986
13-05-1986
18-05-1986

When the if statement occurs the first date is the same so it will increment the numberofRows, the second is also the same but the problem is the if didn't want to be executed at this point.
I use a breakpoint and the value are the same but the if not working. Any idea?


(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

NSString *myDate = [self.date objectAtIndex:section];
int numberOfRows = 0;
  for (int i = 0; i [self.allDates count]; i++) {
 Date *dateFiche = (Date *)[self.allDates objectAtIndex:i];
 NSString *dateInFiche = [[dateFiche sDate] substringWithRange: NSMakeRange( 0,10)]; 
 if ( dateInFiche == myDate ) 
 {
  numberOfRows = numberOfRows+1;
 }
  }
 return numberOfRows;
}
+2  A: 

Your if statement is using == to compare two strings directly. That will only compare the values of the pointers, not the actual contents of the string. Try this instead:

if ([dateInFiche isEqualToString:myDate]) {
    ....
quixoto
thanks thats what I was looking for ~
ludo
+4  A: 

Well, that's not going to work because you're comparing a pointer to an NSString object directly with a pointer to another NSString object. This is akin to:

void *someBuf = calloc (100, 1);
void *anotherBuf = calloc (100, 1);

memcpy (someBuf, "test", 4);
memcpy (anotherBuf, "test", 4);

if (someBuf == anotherBuf)
{
    // won't branch even though their contents are identical
    ...

You can't compare the pointers themselves, you have to compare their contents. You can do this with NSString's isEqualToString:.

if ([firstString isEqualToString:secondString])
{
    // will branch only if the strings have the same content
    ...
dreamlax