From 850756d307214a361986070300fbc930ab536e05 Mon Sep 17 00:00:00 2001 From: viktart Date: Wed, 5 Oct 2022 23:24:33 +0200 Subject: [PATCH 1/4] Add Array.init(numberOfElements:,element:) initializer --- CHANGELOG.md | 3 ++- .../SwiftStdlib/ArrayExtensions.swift | 19 +++++++++++++++++++ .../ArrayExtensionsTests.swift | 6 ++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index efb445e9e..b30b4dbe0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,8 @@ The changelog for **SwifterSwift**. Also see the [releases](https://github.com/S ### Added - **UIButton** - Added `setBackgroundColor(_:for:)` method for setting background color for the specified UI state. [#1041](https://github.com/SwifterSwift/SwifterSwift/pull/1041) by [TwizzyIndy](https://github.com/TwizzyIndy) - +- **Array** + - Added `init(numberOfElements:element:)` initializer for creating an array of a given size with a closure. [#1051](https://github.com/SwifterSwift/SwifterSwift/pull/1051) by [viktart](https://github.com/viktart) ### Fixed - **Digest** - `DigestExtensions.swift` would not compile on Xcode 14 due to an `ambiguous use of 'makeIterator()'` error. [#1042](https://github.com/SwifterSwift/SwifterSwift/issues/1042) by [theedov](https://github.com/theedov) diff --git a/Sources/SwifterSwift/SwiftStdlib/ArrayExtensions.swift b/Sources/SwifterSwift/SwiftStdlib/ArrayExtensions.swift index d3e3f6386..27e48c98b 100755 --- a/Sources/SwifterSwift/SwiftStdlib/ArrayExtensions.swift +++ b/Sources/SwifterSwift/SwiftStdlib/ArrayExtensions.swift @@ -1,5 +1,24 @@ // ArrayExtensions.swift - Copyright 2020 SwifterSwift +// MARK: - Initializers + +public extension Array { + /// SwifterSwift: Creates an array with specified number of elements, for each element it calls specified closure. + /// - Parameters: + /// - numberOfElements: The number of elements in the new array. + /// - element: A closure that initializes each element. + /// - Parameter *index*: An index of initialized element in the array. + /// - Returns: element of the array. + init(count: Int, element: (Int) -> Element) { + self.init(unsafeUninitializedCapacity: count) { buffer, initializedCount in + for index in 0.. Date: Sun, 9 Oct 2022 11:16:55 +0200 Subject: [PATCH 2/4] Update CHANGELOG.md Co-authored-by: Luciano Almeida --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b30b4dbe0..c2b8ece97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ The changelog for **SwifterSwift**. Also see the [releases](https://github.com/S - **UIButton** - Added `setBackgroundColor(_:for:)` method for setting background color for the specified UI state. [#1041](https://github.com/SwifterSwift/SwifterSwift/pull/1041) by [TwizzyIndy](https://github.com/TwizzyIndy) - **Array** - - Added `init(numberOfElements:element:)` initializer for creating an array of a given size with a closure. [#1051](https://github.com/SwifterSwift/SwifterSwift/pull/1051) by [viktart](https://github.com/viktart) + - Added `init(count:element:)` initializer for creating an array of a given size with a closure. [#1051](https://github.com/SwifterSwift/SwifterSwift/pull/1051) by [viktart](https://github.com/viktart) ### Fixed - **Digest** - `DigestExtensions.swift` would not compile on Xcode 14 due to an `ambiguous use of 'makeIterator()'` error. [#1042](https://github.com/SwifterSwift/SwifterSwift/issues/1042) by [theedov](https://github.com/theedov) From 0b3762da2675dcd18fe098bfe67353cfd9df31d8 Mon Sep 17 00:00:00 2001 From: Victor Telitsyn <8170445+viktart@users.noreply.github.com> Date: Tue, 15 Nov 2022 11:56:25 +0100 Subject: [PATCH 3/4] Fix docstring in Sources/SwifterSwift/SwiftStdlib/ArrayExtensions.swift Co-authored-by: Guy Kogus --- Sources/SwifterSwift/SwiftStdlib/ArrayExtensions.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/SwifterSwift/SwiftStdlib/ArrayExtensions.swift b/Sources/SwifterSwift/SwiftStdlib/ArrayExtensions.swift index 27e48c98b..3da68610e 100755 --- a/Sources/SwifterSwift/SwiftStdlib/ArrayExtensions.swift +++ b/Sources/SwifterSwift/SwiftStdlib/ArrayExtensions.swift @@ -5,7 +5,7 @@ public extension Array { /// SwifterSwift: Creates an array with specified number of elements, for each element it calls specified closure. /// - Parameters: - /// - numberOfElements: The number of elements in the new array. + /// - count: The number of elements in the new array. /// - element: A closure that initializes each element. /// - Parameter *index*: An index of initialized element in the array. /// - Returns: element of the array. From 2051717784a29cf8c726a7fd28af5027a31b960f Mon Sep 17 00:00:00 2001 From: viktart Date: Mon, 21 Nov 2022 17:35:19 +0100 Subject: [PATCH 4/4] Fix Array.init(count:, element:) - make it rethrowing --- Sources/SwifterSwift/SwiftStdlib/ArrayExtensions.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sources/SwifterSwift/SwiftStdlib/ArrayExtensions.swift b/Sources/SwifterSwift/SwiftStdlib/ArrayExtensions.swift index 3da68610e..1bedcb325 100755 --- a/Sources/SwifterSwift/SwiftStdlib/ArrayExtensions.swift +++ b/Sources/SwifterSwift/SwiftStdlib/ArrayExtensions.swift @@ -9,10 +9,10 @@ public extension Array { /// - element: A closure that initializes each element. /// - Parameter *index*: An index of initialized element in the array. /// - Returns: element of the array. - init(count: Int, element: (Int) -> Element) { - self.init(unsafeUninitializedCapacity: count) { buffer, initializedCount in + init(count: Int, element: (Int) throws -> Element) rethrows { + try self.init(unsafeUninitializedCapacity: count) { buffer, initializedCount in for index in 0..