How to compare two strings in Swift? Analog of isEqualToString from Objective-C
Hello everyone! I just started learning Swift after Objective-C and encountered a simple but important problem.
In Objective-C, for comparing strings I always used the `isEqualToString:` method. Here's how it looked:
```objc
NSString *str1 = @"Hello";
NSString *str2 = @"Hello";
if ([str1 isEqualToString:str2]) {
NSLog(@"Strings are equal");
}
```
Now I'm switching to Swift and don't understand how to properly compare strings. I tried using the `==` operator, but I'm not sure if it's correct. In Objective-C, `==` compares pointers, not string contents, and I'm afraid it might be the same in Swift.
I also saw that Swift has the `NSString.compare()` method, but it returns `ComparisonResult`, not `Bool`. Is there a direct analog of `isEqualToString`?
My current Swift code looks like this:
No answers yet.
Log in to leave an answer
Blogs