mikkeljuhl mikkeljuhl - 4 months ago 31
Objective-C Question

Compare strings in Objective-C


Possible Duplicate:

How do I compare strings in objective c?

Objective-C If statement?




When I try to compare strings in Objective-C using the == factor I am not getting any result. What I am doing is having an XML document and then retrieving the value "day" and then comparing those. But as I said nothing happens.

XML Code:

<dagensretlist>
<dagensret>
<day>mandag</day>
<date>Mandag 2. juli</date>
<name>Kylling fillet med rosti og salat</name>
<description>Den sprodeste kyllingfillet med Antons piksved</description>
</dagensret>

<dagensret>
<day>tirsdag</day>
<date>Tirsdag 3. juli</date>
<name>Kylling fillet med rosti og salat</name>
<description>Den sprodeste kyllingfillet med Antons piksved</description>
</dagensret>

<dagensret>
<day>onsdag</day>
<date>Onsdag 4. juli</date>
<name>Kylling fillet med rosti og salat</name>
<description>Den sprodeste kyllingfillet med Antons piksved</description>
</dagensret>

<dagensret>
<day>torsdag</day>
<date>Torsdag 5. juli</date>
<name>Kylling fillet med rosti og salat</name>
<description>Den sprodeste kyllingfillet med Antons piksved</description>
</dagensret>

<dagensret>
<day>fredag</day>
<date>Fredag 6. juli</date>
<name>Kylling fillet med rosti og salat</name>
<description>Den sprodeste kyllingfillet med Antons piksved</description>
</dagensret>
</dagensretlist>


The code itself:

- (void)getCurrencyList {
    NSArray *currencyList = [self getAllItems:@"//dagensret" fileName:@"dagensret.xml"];
    int numberOfCurrencies = [currencyList count];
    
    NSString *name, *date, *description, *day;
    if (numberOfCurrencies > 0) {
        for (int i = 0; i < numberOfCurrencies; i++) {
            
            name            = [[currencyList objectAtIndex:i] objectForKey:@"name"];
            day             = [[currencyList objectAtIndex:i] objectForKey:@"day"];
            date            = [[currencyList objectAtIndex:i] objectForKey:@"date"];
            description     = [[currencyList objectAtIndex:i] objectForKey:@"description"];
            
            if(day==@"1"){
                NSLog(@"day: %@, date: %@, name: %@, description: %@", day, date, name, description);
                NSLog(@"1");
            }
            if(day==@"tirsdag"){
                NSLog(@"day: %@, date: %@, name: %@, description: %@", day, date, name, description); 
            }
            if(day==@"onsdag"){
                NSLog(@"day: %@, date: %@, name: %@, description: %@", day, date, name, description); 
            } 
            if(day==@"torsdag"){
                NSLog(@"day: %@, date: %@, name: %@, description: %@", day, date, name, description); 
            } 
            if(day==@"fredag"){
                NSLog(@"day: %@, date: %@, name: %@, description: %@", day, date, name, description); 
            }             
            
        }
    }
}


Is this a wrong way to compare strings to a variable?

Thank you!

MJB MJB
Answer

Use isEqualToString from NSString class to compare strings

documentation