[Swift] I thought about compare

This is a story of the thoughts of the first scholar, the author, when he first met compare.

Compare here refers to Swift's String class instance method compare.

What's the problem with the return value of compare

It's a Comparison Result. Yes, that's it.

It is the beginners who do not become ... I was one of them. The Apple Developer Documents also says ** Return Value ** prominently!

I'm stumbling in such a rudimentary place, but the process of trial and error was a good study, so I decided to leave it here in the form of output.

Where did you meet?

Introduction to Swift Practice 3rd Edition P53 Advanced operation by Foundation With sample code

It will be the above part. I think there are many beginners who pick up this book for learning swift. I got stuck when I saw the following code that suddenly appeared above.

//P53 sample code
import Foundation //compare is a method provided by a library called Foundation

//Comparison of order between two strings
let options = String.CompareOptions.caseInsensitive
let order = "abc".compare("ABC", options: options)
order == ComparisonResult.orderedSame //true

In the above code, the compare (_: option) method validates the order between the two strings. --Omitted--The result is a value of .orderSame, which means the order is the same.

It's hard to understand just by reading it.

Try code reading

First line

let options = String.CompareOptions.caseInsensitive

This is an option provided by the Compare method that is not case sensitive. This is defined as a constant options.

2nd line

let order = "abc".compare("ABC", options: options)

On the right side, the compare method is used for the character string "abc" to compare it with "ABC" in (). At that time, it receives the options defined earlier as an argument. The result obtained here is defined as the constant order.

3rd line

order == ComparisonResult.orderedSame

The order and ComparisonResult.orderedSame defined in the second line are applied to the comparison operator, and true is returned.

Think about the details

Based on the above, I simplified the code a little. The options are omitted because they do not seem to have anything to do with the question.

let order = "abc".compare("abc")
order == ComparisonResult.orderedSame // true

In other words, the return value of " abc ".compare (" abc ") is ComparisonResult.orderedSame?

I checked it.

let hoge = "abc".compare("abc")
print(hoge) // NSComparisonResult

Execution result ・ ・ ・ NSComparisonResult

What is Comparison Result?

It seems that it is necessary to understand this guy who appeared with a face that does not eat even with the sample code.

ComparisonResult

Upon examination, it was found that ComparisonResult is a ** enumeration ** and has three case constants.

enum ComparisonResult: Int {
    case orderedAscending = -1
    case orderedSame = 0
    case orderedDescending = 1
}

This means that the compare method will return a ComparisonResult after getting one of the cases.

--case orderedAscending ・ ・ ・ < --case orderedSame ・ ・ ・ = --case orderedDescending ・ ・ ・ >

Since the comparison target was equal this time, we got the value orderedSame and This means that ComparisonResult.orderedSame has been returned.

Return value of compare

So let's go back to the code at the beginning. Since it's a big deal, I'll change what was "ABC" to "DEF".

let options = String.CompareOptions.caseInsensitive
let order = "abc".compare("DEF", options: options)
order == ComparisonResult.orderedAscending
// true

The return value of " abc ".compare ("DEF ", options: options) on the second line is ComparisonResult.orderedAscending, so it is true. The comparison result is abc <DEF.

Later talk

Even if I write it myself now, it's a content that makes me wonder, "Why did you care about this?"

The reason I can think of it may be that I was confused that the return value of the method was an enumeration type.

It's another method, but I imagined the following simple behavior. Code and execution results as I expected

//Code and execution results as I expected
"abc".isEqual("abc")
// true

This time as well, I had a hard time understanding because of the prejudice that the above-mentioned form did not get out of my mind and that "comparative methods return true / false".

** Conclusion: I should have read the official documentation immediately. ** **

That's it.

Thank you for reading so far m (_ _) m

Recommended Posts

[Swift] I thought about compare
[Swift] About generics
[Swift] [Beginner] I searched a lot about #selector
Compare objects in Swift
[Swift] About enumeration types
[Swift] About protocol extension
About [Cocoa] [Swift] AirPlay 2
[Ruby] I thought about the difference between each_with_index and each.with_index
[Spring boot] I thought about testable code by DI
What I researched about Java 8
[Swift] [Beginner]] About range operators
[Swift] Summary about Bool type
[Swift] About asynchronous processing "Operation"
What I researched about Java 7
10 Things I Hate About Android
Compare Java 8 Optional with Swift
[Java] I thought about the merits and uses of "interface"
What I learned about Kotlin
What I researched about Java 5
I thought about how to use Swift's willSet did Set properly.
I thought about the strategy of introducing Combine in iOS development
What I thought about when I started migrating from Java to Kotlin
Swift: I want to chain arrays
[Swift] "for-in statement" about iterative processing
What I researched about Java learning
When updating my own application, I seriously thought about the package structure
I thought about how to make it serverless while avoiding vendor lock-in
I thought about the best way to create a ValueObject in Ruby