[swift-users] See documentation comment for discussion

Svein Halvor Halvorsen svein.h at lvor.halvorsen.cc
Sat Jun 18 03:17:00 CDT 2016


let firstLetter = myString.characters.first

lør. 18. jun. 2016 kl. 04.38 skrev Brent Royal-Gordon via swift-users <
swift-users at swift.org>:

> > I've previously used myString[0...1] to get the first character of a
> string.
>
> I'm not sure how, because this has never worked in Swift. Strings use
> opaque indices which specifically prevent you from doing this, so you have
> never been able to subscript a string with integers or ranges of integers.
> It's done this way because indexing into a Unicode string intrinsically
> requires walking through the data structure and counting characters one at
> a time; the lack of integer indices is meant to force you to explicitly
> write `index(after:)` or `index(_:offsetBy:)` (formerly `successor()` or
> `advancedBy(_:)`) in your code to make this cost obvious.
>
> > What documentation is this referring to? There's no right-click
> definition available, nothing in Navigator, and Google fails to turn up
> anything.
>
> The standard library includes definitions of these subscripts which are
> meant to direct you to documentation explaining why they're not supported,
> but these definitions don't show up in the generated interfaces. Presumably
> Xcode is removing all APIs marked "unavailable". The doc comment it's
> trying to direct you to is here <
> https://github.com/apple/swift/blob/b8401e1fde52d95e5a8ce7b043a3c5a3bcf72181/stdlib/public/core/UnavailableStringAPIs.swift.gyb#L15
> >:
>
>   /// Subscripting strings with integers is not available.
>   ///
>   /// The concept of "the `i`th character in a string" has
>   /// different interpretations in different libraries and system
>   /// components.  The correct interpretation should be selected
>   /// according to the use case and the APIs involved, so `String`
>   /// cannot be subscripted with an integer.
>   ///
>   /// Swift provides several different ways to access the character
>   /// data stored inside strings.
>   ///
>   /// - `String.utf8` is a collection of UTF-8 code units in the
>   ///   string. Use this API when converting the string to UTF-8.
>   ///   Most POSIX APIs process strings in terms of UTF-8 code units.
>   ///
>   /// - `String.utf16` is a collection of UTF-16 code units in
>   ///   string.  Most Cocoa and Cocoa touch APIs process strings in
>   ///   terms of UTF-16 code units.  For example, instances of
>   ///   `NSRange` used with `NSAttributedString` and
>   ///   `NSRegularExpression` store substring offsets and lengths in
>   ///   terms of UTF-16 code units.
>   ///
>   /// - `String.unicodeScalars` is a collection of Unicode scalars.
>   ///   Use this API when you are performing low-level manipulation
>   ///   of character data.
>   ///
>   /// - `String.characters` is a collection of extended grapheme
>   ///   clusters, which are an approximation of user-perceived
>   ///   characters.
>   ///
>   /// Note that when processing strings that contain human-readable
>   /// text, character-by-character processing should be avoided to
>   /// the largest extent possible.  Use high-level locale-sensitive
>   /// Unicode algorithms instead, for example,
>   /// `String.localizedStandardCompare()`,
>   /// `String.localizedLowercaseString`,
>   /// `String.localizedStandardRangeOfString()` etc.
>
>
> --
> Brent Royal-Gordon
> Architechies
>
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160618/08b952c9/attachment.html>


More information about the swift-users mailing list