Compares the string using the specified options and returns the lexical ordering for the range.
SDKs
- iOS 2.0+
- macOS 10.0+
- Mac Catalyst 13.0+
- tvOS 9.0+
- watchOS 2.0+
Framework
- Foundation
Declaration
func compare(_ string: String, options mask: NSString.Compare Options = [], range rangeOfReceiverToCompare: NSRange, locale: Any?) -> Comparison Result
Parameters
aStringThe string with which to compare the range of the receiver specified by
range.This value must not be
nil. If this value isnil, the behavior is undefined and may change in future versions of macOS.maskOptions for the search—you can combine any of the following using a C bitwise OR operator:
NSCase,Insensitive Search NSLiteral,Search NSNumeric.Search See String Programming Guide for details on these options.
rangeThe range of the receiver over which to perform the comparison. The range must not exceed the bounds of the receiver.
Important
Raises an
NSRangeifException rangeexceeds the bounds of the receiver.locale
Return Value
Returns an Comparison value that indicates the lexical ordering of a specified range within the receiver and a given string. Comparison if the substring of the receiver given by range precedes a in lexical ordering for the locale given in dict, Comparison if the substring of the receiver and a are equivalent in lexical value, and Comparison if the substring of the receiver follows a.
Discussion
The locale argument affects both equality and ordering algorithms. For example, in some locales, accented characters are ordered immediately after the base; other locales order them after “z”.