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
.
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.
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.
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.
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
.
order == ComparisonResult.orderedSame
The order
and ComparisonResult.orderedSame
defined in the second line are applied to the comparison operator, and true is returned.
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
It seems that it is necessary to understand this guy who appeared with a face that does not eat even with the sample code.
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.
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
.
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".
That's it.
Thank you for reading so far m (_ _) m
Recommended Posts