2

I wrote the following code:

   if (depSelectedIndice > -1 && comSelectedIndice> -1)
        {
            NSLog(@"depart elemet : %d ",depSelectedIndice);
            NSLog(@"depart elemet : %d ",comSelectedIndice);
            NSLog(@"ok1");
            NSString *choosedDate =[NSString stringWithFormat:@"%@%@",[deparatureDates objectAtIndex:depSelecif (depSelectedIndice > -1 && comSelectedIndice> -1)
    {
        NSLog(@"depart elemet : %d ",depSelectedIndice);
        NSLog(@"depart elemet : %d ",comSelectedIndice);
        NSLog(@"ok1");
        NSString *choosedDate =[NSString stringWithFormat:@"%@%@",[deparatureDates objectAtIndex:depSelectedIndice], [goingBackDates objectAtIndex:comSelectedIndice]];
        NSLog(@"0000000000001");

        NSLog(@" number of element : %d", [allCombinations count]);
//        for (int j=0; j<[allCombinations count]; j++)
//        {
//            NSLog(@"111111111111111111");
//           // NSString *date = [[allCombinations objectAtIndex:j] objectForKey:@"keydate"];
//            NSLog(@"22222222222222222222");
//              if([date isEqualToString:choosedDate])
//              {
//                  depPrice.text=@"1";
//                  comPrice.text=@"1";
//                  price.text=@"3";
//                  
//              }
       // }
    }

allCombinations is an NSArray declared in .h, I have initilase and used it in another method. I can't use in in this method? :/

But I have a crash. I don't really know where the problem is but I think it's when I compare if(date==choosedDate)? Help please

dredful
  • 4,378
  • 2
  • 35
  • 54
user761812
  • 245
  • 2
  • 5
  • 15
  • Can you add your stack trace when the crash occurs? – dredful May 25 '11 at 01:42
  • @dredful @dredful @Chris Thank you all . i edited my post . The problem come from allCombinations . I have 0000000000001 in the consol.The crach is when i make [allCombinations count] .There are no error in the consol to post it here :( help pleeease – user761812 May 25 '11 at 01:51
  • "allCombinations is an NSArray declared in .h , I have initilase and use it in another method . I can't use in in this method ? :/" - You will have to show us the code/methods that relate to `allCombinations` so we can see if you are doing something incorrect with it. – dredful May 25 '11 at 02:19

3 Answers3

26

When you use an == on pointers like NSString * it is comparing memory addresses, not comparing the value of strings.

The following will actually compare the string values:

if([date isEqualToString:choosedDate])
dredful
  • 4,378
  • 2
  • 35
  • 54
3

In Objective C better way to compare two string is:

NSString *string1 = <your string>;
NSString *string2 = <your string>;

if ([string1 caseInsensitiveCompare:string2] == NSOrderedSame) {
    //strings are same
} else {
    //strings are not same
}
loganathan
  • 2,056
  • 2
  • 23
  • 34
2

In addition to using [date isEqualToString:choosedDate] instead of date==choosedDate, my initial reaction would be to make sure that depSelectedIndice and comSelectedIndice do not refer to elements past the end of deparatureDates and goingBackDates in the following line.

NSString *choosedDate =[NSString stringWithFormat:@"%@%@",[deparatureDates objectAtIndex:depSelectedIndice], [goingBackDates objectAtIndex:comSelectedIndice]];

I don't whether depPrice, comPrice, and price were allocated correctly, nor what their types are, but they could be causing you problems, as well.

Chris Frederick
  • 5,482
  • 3
  • 36
  • 44