@@ -448,21 +448,20 @@ public struct IndexingIterator<
448
448
/// at a specific position when using a collection.
449
449
///
450
450
/// For example, if you want to print only the first word in a string, search
451
- /// for the index of the first space and then create a subsequence up to that
451
+ /// for the index of the first space, and then create a subsequence up to that
452
452
/// position.
453
453
///
454
454
/// let text = "Buffalo buffalo buffalo buffalo."
455
- /// if let firstSpace = text.characters. index(of: " ") {
456
- /// print(String( text.characters.prefix(upTo: firstSpace)) )
455
+ /// if let firstSpace = text.index(of: " ") {
456
+ /// print(text[..< firstSpace] )
457
457
/// }
458
458
/// // Prints "Buffalo"
459
459
///
460
- /// The `firstSpace` constant is an index into the `text.characters`
461
- /// collection. `firstSpace` is the position of the first space in the
462
- /// collection. You can store indices in variables, and pass them to
463
- /// collection algorithms or use them later to access the corresponding
464
- /// element. In the example above, `firstSpace` is used to extract the prefix
465
- /// that contains elements up to that index.
460
+ /// The `firstSpace` constant is an index into the `text` string---the position
461
+ /// of the first space in the string. You can store indices in variables, and
462
+ /// pass them to collection algorithms or use them later to access the
463
+ /// corresponding element. In the example above, `firstSpace` is used to
464
+ /// extract the prefix that contains elements up to that index.
466
465
///
467
466
/// You can pass only valid indices to collection operations. You can find a
468
467
/// complete set of a collection's valid indices by starting with the
@@ -486,7 +485,7 @@ public struct IndexingIterator<
486
485
/// Here's an example of accessing the first character in a string through its
487
486
/// subscript:
488
487
///
489
- /// let firstChar = text.characters [text.characters .startIndex]
488
+ /// let firstChar = text[text.startIndex]
490
489
/// print(firstChar)
491
490
/// // Prints "B"
492
491
///
@@ -496,40 +495,33 @@ public struct IndexingIterator<
496
495
/// using the `first` property, which has the value of the first element of
497
496
/// the collection, or `nil` if the collection is empty.
498
497
///
499
- /// print(text.characters. first)
498
+ /// print(text.first)
500
499
/// // Prints "Optional("B")"
501
500
///
502
501
/// Accessing Slices of a Collection
503
502
/// ================================
504
503
///
505
504
/// You can access a slice of a collection through its ranged subscript or by
506
- /// calling methods like `prefix(_ :)` or `suffix(from :)`. A slice of a
505
+ /// calling methods like `prefix(while :)` or `suffix(_ :)`. A slice of a
507
506
/// collection can contain zero or more of the original collection's elements
508
507
/// and shares the original collection's semantics.
509
508
///
510
- /// The following example, creates a `firstWord` constant by using the
511
- /// `prefix(_:)` method to get a slice of the `text` string's `characters`
512
- /// view.
509
+ /// The following example creates a `firstWord` constant by using the
510
+ /// `prefix(where:)` method to get a slice of the `text` string.
513
511
///
514
- /// let firstWord = text.characters. prefix(7 )
515
- /// print(String( firstWord) )
512
+ /// let firstWord = text.prefix(while: { $0 != " " } )
513
+ /// print(firstWord)
516
514
/// // Prints "Buffalo"
517
515
///
518
- /// You can retrieve the same slice using other methods, such as finding the
519
- /// terminating index, and then using the `prefix(upTo:)` method, which takes
520
- /// an index as its parameter, or by using the view's ranged subscript.
516
+ /// You can retrieve the same slice using the string's ranged subscript, which
517
+ /// takes a range expression.
521
518
///
522
- /// if let firstSpace = text.characters.index(of: " ") {
523
- /// print(text.characters.prefix(upTo: firstSpace))
524
- /// // Prints "Buffalo"
525
- ///
526
- /// let start = text.characters.startIndex
527
- /// print(text.characters[start..<firstSpace])
519
+ /// if let firstSpace = text.index(of: " ") {
520
+ /// print(text[..<firstSpace]
528
521
/// // Prints "Buffalo"
529
522
/// }
530
523
///
531
- /// The retrieved slice of `text.characters` is equivalent in each of these
532
- /// cases.
524
+ /// The retrieved slice of `text` is equivalent in each of these cases.
533
525
///
534
526
/// Slices Share Indices
535
527
/// --------------------
@@ -567,10 +559,9 @@ public struct IndexingIterator<
567
559
/// -----------------------------------
568
560
///
569
561
/// A slice inherits the value or reference semantics of its base collection.
570
- /// That is, when working with a slice of a mutable
571
- /// collection that has value semantics, such as an array, mutating the
572
- /// original collection triggers a copy of that collection, and does not
573
- /// affect the contents of the slice.
562
+ /// That is, when working with a slice of a mutable collection that has value
563
+ /// semantics, such as an array, mutating the original collection triggers a
564
+ /// copy of that collection, and does not affect the contents of the slice.
574
565
///
575
566
/// For example, if you update the last element of the `absences` array from
576
567
/// `0` to `2`, the `secondHalf` slice is unchanged.
@@ -598,7 +589,7 @@ public struct IndexingIterator<
598
589
/// indices or the view itself is being iterated.
599
590
///
600
591
/// let word = "Swift"
601
- /// for character in word.characters {
592
+ /// for character in word {
602
593
/// print(character)
603
594
/// }
604
595
/// // Prints "S"
@@ -607,8 +598,8 @@ public struct IndexingIterator<
607
598
/// // Prints "f"
608
599
/// // Prints "t"
609
600
///
610
- /// for i in word.characters. indices {
611
- /// print(word.characters [i])
601
+ /// for i in word.indices {
602
+ /// print(word[i])
612
603
/// }
613
604
/// // Prints "S"
614
605
/// // Prints "w"
@@ -798,6 +789,15 @@ public protocol Collection : _Indexable, Sequence
798
789
/// print(numbers.prefix(upTo: numbers.startIndex))
799
790
/// // Prints "[]"
800
791
///
792
+ /// Using the `prefix(upTo:)` method is equivalent to using a partial
793
+ /// half-open range as the collection's subscript. The subscript notation is
794
+ /// preferred over `prefix(upTo:)`.
795
+ ///
796
+ /// if let i = numbers.index(of: 40) {
797
+ /// print(numbers[..<i])
798
+ /// }
799
+ /// // Prints "[10, 20, 30]"
800
+ ///
801
801
/// - Parameter end: The "past the end" index of the resulting subsequence.
802
802
/// `end` must be a valid index of the collection.
803
803
/// - Returns: A subsequence up to, but not including, the `end` position.
@@ -825,6 +825,15 @@ public protocol Collection : _Indexable, Sequence
825
825
/// print(numbers.suffix(from: numbers.endIndex))
826
826
/// // Prints "[]"
827
827
///
828
+ /// Using the `suffix(from:)` method is equivalent to using a partial range
829
+ /// from the index as the collection's subscript. The subscript notation is
830
+ /// preferred over `suffix(from:)`.
831
+ ///
832
+ /// if let i = numbers.index(of: 40) {
833
+ /// print(numbers[i...])
834
+ /// }
835
+ /// // Prints "[40, 50, 60]"
836
+ ///
828
837
/// - Parameter start: The index at which to start the resulting subsequence.
829
838
/// `start` must be a valid index of the collection.
830
839
/// - Returns: A subsequence starting at the `start` position.
@@ -846,6 +855,15 @@ public protocol Collection : _Indexable, Sequence
846
855
/// }
847
856
/// // Prints "[10, 20, 30, 40]"
848
857
///
858
+ /// Using the `prefix(through:)` method is equivalent to using a partial
859
+ /// closed range as the collection's subscript. The subscript notation is
860
+ /// preferred over `prefix(through:)`.
861
+ ///
862
+ /// if let i = numbers.index(of: 40) {
863
+ /// print(numbers[...i])
864
+ /// }
865
+ /// // Prints "[10, 20, 30, 40]"
866
+ ///
849
867
/// - Parameter end: The index of the last element to include in the
850
868
/// resulting subsequence. `end` must be a valid index of the collection
851
869
/// that is not equal to the `endIndex` property.
@@ -864,7 +882,7 @@ public protocol Collection : _Indexable, Sequence
864
882
/// through the elements of the collection.
865
883
///
866
884
/// let horseName = "Silver"
867
- /// if horseName.characters. isEmpty {
885
+ /// if horseName.isEmpty {
868
886
/// print("I've been through the desert on a horse with no name.")
869
887
/// } else {
870
888
/// print("Hi ho, \(horseName)!")
@@ -1319,7 +1337,7 @@ extension Collection {
1319
1337
/// through the elements of the collection.
1320
1338
///
1321
1339
/// let horseName = "Silver"
1322
- /// if horseName.characters. isEmpty {
1340
+ /// if horseName.isEmpty {
1323
1341
/// print("I've been through the desert on a horse with no name.")
1324
1342
/// } else {
1325
1343
/// print("Hi ho, \(horseName)!")
@@ -1420,7 +1438,7 @@ extension Collection {
1420
1438
/// let cast = ["Vivien", "Marlon", "Kim", "Karl"]
1421
1439
/// let lowercaseNames = cast.map { $0.lowercaseString }
1422
1440
/// // 'lowercaseNames' == ["vivien", "marlon", "kim", "karl"]
1423
- /// let letterCounts = cast.map { $0.characters. count }
1441
+ /// let letterCounts = cast.map { $0.count }
1424
1442
/// // 'letterCounts' == [6, 6, 3, 4]
1425
1443
///
1426
1444
/// - Parameter transform: A mapping closure. `transform` accepts an
@@ -1622,6 +1640,15 @@ extension Collection {
1622
1640
/// print(numbers.prefix(upTo: numbers.startIndex))
1623
1641
/// // Prints "[]"
1624
1642
///
1643
+ /// Using the `prefix(upTo:)` method is equivalent to using a partial
1644
+ /// half-open range as the collection's subscript. The subscript notation is
1645
+ /// preferred over `prefix(upTo:)`.
1646
+ ///
1647
+ /// if let i = numbers.index(of: 40) {
1648
+ /// print(numbers[..<i])
1649
+ /// }
1650
+ /// // Prints "[10, 20, 30]"
1651
+ ///
1625
1652
/// - Parameter end: The "past the end" index of the resulting subsequence.
1626
1653
/// `end` must be a valid index of the collection.
1627
1654
/// - Returns: A subsequence up to, but not including, the `end` position.
@@ -1652,6 +1679,15 @@ extension Collection {
1652
1679
/// print(numbers.suffix(from: numbers.endIndex))
1653
1680
/// // Prints "[]"
1654
1681
///
1682
+ /// Using the `suffix(from:)` method is equivalent to using a partial range
1683
+ /// from the index as the collection's subscript. The subscript notation is
1684
+ /// preferred over `suffix(from:)`.
1685
+ ///
1686
+ /// if let i = numbers.index(of: 40) {
1687
+ /// print(numbers[i...])
1688
+ /// }
1689
+ /// // Prints "[40, 50, 60]"
1690
+ ///
1655
1691
/// - Parameter start: The index at which to start the resulting subsequence.
1656
1692
/// `start` must be a valid index of the collection.
1657
1693
/// - Returns: A subsequence starting at the `start` position.
@@ -1676,6 +1712,15 @@ extension Collection {
1676
1712
/// }
1677
1713
/// // Prints "[10, 20, 30, 40]"
1678
1714
///
1715
+ /// Using the `prefix(through:)` method is equivalent to using a partial
1716
+ /// closed range as the collection's subscript. The subscript notation is
1717
+ /// preferred over `prefix(through:)`.
1718
+ ///
1719
+ /// if let i = numbers.index(of: 40) {
1720
+ /// print(numbers[...i])
1721
+ /// }
1722
+ /// // Prints "[10, 20, 30, 40]"
1723
+ ///
1679
1724
/// - Parameter end: The index of the last element to include in the
1680
1725
/// resulting subsequence. `end` must be a valid index of the collection
1681
1726
/// that is not equal to the `endIndex` property.
@@ -1701,25 +1746,20 @@ extension Collection {
1701
1746
/// that was originally separated by one or more spaces.
1702
1747
///
1703
1748
/// let line = "BLANCHE: I don't want realism. I want magic!"
1704
- /// print(line.characters.split(whereSeparator: { $0 == " " })
1705
- /// .map(String.init))
1749
+ /// print(line.split(whereSeparator: { $0 == " " }))
1706
1750
/// // Prints "["BLANCHE:", "I", "don\'t", "want", "realism.", "I", "want", "magic!"]"
1707
1751
///
1708
1752
/// The second example passes `1` for the `maxSplits` parameter, so the
1709
1753
/// original string is split just once, into two new strings.
1710
1754
///
1711
- /// print(
1712
- /// line.characters.split(
1713
- /// maxSplits: 1, whereSeparator: { $0 == " " }
1714
- /// ).map(String.init))
1755
+ /// print(line.split(maxSplits: 1, whereSeparator: { $0 == " " }))
1715
1756
/// // Prints "["BLANCHE:", " I don\'t want realism. I want magic!"]"
1716
1757
///
1717
1758
/// The final example passes `false` for the `omittingEmptySubsequences`
1718
1759
/// parameter, so the returned array contains empty strings where spaces
1719
1760
/// were repeated.
1720
1761
///
1721
- /// print(line.characters.split(omittingEmptySubsequences: false, whereSeparator: { $0 == " " })
1722
- /// .map(String.init))
1762
+ /// print(line.split(omittingEmptySubsequences: false, whereSeparator: { $0 == " " }))
1723
1763
/// // Prints "["BLANCHE:", "", "", "I", "don\'t", "want", "realism.", "I", "want", "magic!"]"
1724
1764
///
1725
1765
/// - Parameters:
@@ -1801,23 +1841,20 @@ extension Collection where Iterator.Element : Equatable {
1801
1841
/// was originally separated by one or more spaces.
1802
1842
///
1803
1843
/// let line = "BLANCHE: I don't want realism. I want magic!"
1804
- /// print(line.characters.split(separator: " ")
1805
- /// .map(String.init))
1844
+ /// print(line.split(separator: " "))
1806
1845
/// // Prints "["BLANCHE:", "I", "don\'t", "want", "realism.", "I", "want", "magic!"]"
1807
1846
///
1808
1847
/// The second example passes `1` for the `maxSplits` parameter, so the
1809
1848
/// original string is split just once, into two new strings.
1810
1849
///
1811
- /// print(line.characters.split(separator: " ", maxSplits: 1)
1812
- /// .map(String.init))
1850
+ /// print(line.split(separator: " ", maxSplits: 1))
1813
1851
/// // Prints "["BLANCHE:", " I don\'t want realism. I want magic!"]"
1814
1852
///
1815
1853
/// The final example passes `false` for the `omittingEmptySubsequences`
1816
1854
/// parameter, so the returned array contains empty strings where spaces
1817
1855
/// were repeated.
1818
1856
///
1819
- /// print(line.characters.split(separator: " ", omittingEmptySubsequences: false)
1820
- /// .map(String.init))
1857
+ /// print(line.split(separator: " ", omittingEmptySubsequences: false))
1821
1858
/// // Prints "["BLANCHE:", "", "", "I", "don\'t", "want", "realism.", "I", "want", "magic!"]"
1822
1859
///
1823
1860
/// - Parameters:
0 commit comments