What is the Swift equivalent of isEqualToString in Objective-C?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Objective-C, string equality is commonly written with isEqualToString:. In Swift, the direct equivalent for most code is simply the == operator, which compares two String values by content rather than by object identity.
Using == for exact string equality
Swift strings are value types, so equality is expressed in a natural way:
That is the ordinary replacement for Objective-C code like this:
If your only question is "do these two strings contain the same characters in the same order?", == is the right Swift answer.
Comparing optional strings safely
A common source of confusion during migration is optional handling. In Swift, String? values can still be compared directly:
This is convenient because Swift handles the optional comparison rules for you. If one side is nil and the other is not, the result is false. If both are nil, the result is true.
When you need to compare a non-optional string with an optional one, unwrap intentionally:
That keeps the intent clear and avoids accidental force-unwrapping.
Case-insensitive and localized comparisons
== is an exact, case-sensitive comparison. If you need more flexible behavior, use the comparison APIs provided by Swift and Foundation.
For a case-insensitive comparison:
For user-facing text, a localized comparison is often better:
This distinction matters because exact string equality and locale-aware user comparison solve different problems.
Bridging with NSString when needed
Most Swift code should stay with String, but you may still encounter legacy APIs or Cocoa code that expects NSString. Bridging is straightforward:
You can call isEqualToString: on NSString, but in mixed Swift code it is usually cleaner to convert to String and continue with Swift-native equality.
Common Pitfalls
The most common mistake is assuming == performs a case-insensitive comparison. It does not. "Admin" and "admin" are different unless you choose a case-insensitive API explicitly.
Another issue is mixing up equality of string content with identity of an object instance. Objective-C developers sometimes think in terms of objects and selectors, but Swift String equality is about value equality, which is exactly what you want most of the time.
Be careful with optionals as well. Force-unwrapping a String? just to compare it is unnecessary and can crash. Swift can compare optionals directly or you can unwrap safely with if let.
Finally, use locale-aware comparison only when the problem is truly user-facing text comparison. For keys, identifiers, tokens, or protocol values, exact equality is usually the correct rule.
Summary
- The ordinary Swift equivalent of
isEqualToString:is==. - '
==compares string content exactly and is case-sensitive.' - Optional strings can be compared directly, but explicit unwrapping often reads better.
- Use comparison APIs such as
caseInsensitiveCompareorcompare(..., locale: ...)when the match should ignore case or follow locale rules. - In mixed Cocoa code, bridge
NSStringtoStringand keep the comparison Swift-native.
Related reading
- What is the Swift equivalent of respondsToSelector?
- What is the Swift equivalent to Objective-C's synchronized?
- What is the time complexity big O of sortedArrayUsingComparator? iOS/OSX
- what is the usage of in cocoapods
- What is the use of static keyword if let keyword used to define constants/immutables in swift?
- What is the way to quick-switch between tabs in Xcode 4
- What is 'Vary for Traits' in Xcode 8?
- What Java 8 Stream.collect equivalents are available in the standard Kotlin library?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.