-7

I have two string. This strings is not equal but if else statement always saying yes.

My code these;

 PFQuery *bilquery = [PFQuery queryWithClassName:@"Bildirim"];
    PFObject *saat = [bilquery getObjectWithId:@"lQTsXaBvuq"];
    NSString *asd = [saat objectForKey:@"gun"]; //asd is 06 or 07.

    NSDate *currDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"dd"];
    NSString *dateString = [dateFormatter stringFromDate:currDate];
    int d = [dateString intValue] + 1;
    NSString *dplus1 = [NSString stringWithFormat:@"%d",d];
    if ((dateString = asd)) {
        NSLog(@"YES");
    } else {
       NSLog(@"NO");
       // Bla bla bla

    }

NSString asd in my parse database. Where is my correct in if else statement?

Tommy
  • 39,592
  • 10
  • 90
  • 121
Salieh
  • 693
  • 2
  • 7
  • 12
  • 4
    You need to step back and learn the basics of the language. The `=` operator is the assignment operator. To compare two objects you need to use the `isEqual:` method. In the case of two `NSString` objects, use `isEqualToString:`. – rmaddy May 05 '13 at 21:34
  • @Salieh - Once your question has been answered, please do not delete or overwrite your questions with "Thanks" commentary as anyone else coming to find an answer to a similar issue will not see your original question. – Tommy May 05 '13 at 22:07
  • @Tommy I suggested an edit to restore the original content, but moderators say that is inappropriate, so it seems it's fine if Salieh does what he did. –  May 05 '13 at 22:07
  • @Sancho - I rolled the question back to its previous form. I was brought here by reviewing proposed edits. Thanks for the catch, I believe it should be left in its original form as you proposed – Tommy May 05 '13 at 22:08
  • ah. Thank you for rolling back. –  May 05 '13 at 22:09
  • possible duplicate of [Checking for equality in Objective-C](http://stackoverflow.com/questions/2293859/checking-for-equality-in-objective-c) – Kate Gregory May 05 '13 at 23:15

2 Answers2

3
if ((dateString = asd)) 

Here you are assigning asd to dateString, as you use a single =.
Comparisons are made with ==.

But as NSString instances are pointers, you can't use == to compare the string values.
Use:

if( [ str1 isEqualToString: str2 ] )

That being said, I think you should really take some time to learn some Objective-C basics...

Macmade
  • 52,708
  • 13
  • 106
  • 123
1

= is not the operator for testing equality.

== tests for equality, but since you're using pointers, this still wouldn't be correct.

See this answer for more detail (Checking for equality in Objective-C), but you need to use this test:

if([dateString isEqualToString:asd])

Community
  • 1
  • 1