Compare strings from the Internet

Hi everyone, I am trying to read a file on the Internet that says “this is a test” and I want to compare it ... so this is what I got:

NSError *error;
NSURL *theURL = [NSURL URLWithString:@"http://mysite.com/test.asp"];
NSString *test = [NSString stringWithContentsOfURL:theURL encoding:NSASCIIStringEncoding error:&error];

NSLog(@"%@",test); //prints the results.. .does work


if(test == "this is a test"){
    NSLog(@"File read");
} else {
    NSLog(@"Bad file");
}

What am I doing wrong here? I always get a Bad File, but I know that it pulls the text. Thank you all

Damien

+2
source share
3 answers

You also need to check for null and use the isEqualToString function for comparison.

if(test != nil){

    if([test isEqualToString:@"this is a test"]) {
       // equal
    }
    else{
      // not equal
    }
}

else{

   NSLog(@"Bad file");

}
+4
source

If you use ==, you will compare two pointers. Use isEqual:as follows:

if([test isEqual: @"this is a test"]) {
    // equal
}else{
    // not equal
}
0
source

, .

caseInsensitiveCompare:localizedCaseInsensitiveCompare:compare:localizedCompare:compare:options:compare:options:range:compare:options:range:locale:localizedStandardCompare:
0

Source: https://habr.com/ru/post/1793767/


All Articles