From 46a09cf31ae45063aaa7d1d3a47af49716f2c7cf Mon Sep 17 00:00:00 2001 From: nguyen-ta-cuong Date: Sun, 11 Oct 2026 00:26:44 +0700 Subject: [PATCH] fix: correct Swift fixed-size array insertion bounds (#1968) Co-authored-by: Cuong Nguyen Co-authored-by: krahets --- codes/swift/chapter_array_and_linkedlist/array.swift | 2 +- en/codes/swift/chapter_array_and_linkedlist/array.swift | 2 +- ja/codes/swift/chapter_array_and_linkedlist/array.swift | 2 +- ru/codes/swift/chapter_array_and_linkedlist/array.swift | 2 +- zh-hant/codes/swift/chapter_array_and_linkedlist/array.swift | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/codes/swift/chapter_array_and_linkedlist/array.swift b/codes/swift/chapter_array_and_linkedlist/array.swift index 5b0024931..dab2c9214 100644 --- a/codes/swift/chapter_array_and_linkedlist/array.swift +++ b/codes/swift/chapter_array_and_linkedlist/array.swift @@ -28,7 +28,7 @@ func extend(nums: [Int], enlarge: Int) -> [Int] { /* 在数组的索引 index 处插入元素 num */ func insert(nums: inout [Int], num: Int, index: Int) { // 把索引 index 以及之后的所有元素向后移动一位 - for i in nums.indices.dropFirst(index).reversed() { + for i in stride(from: nums.count - 1, to: index, by: -1) { nums[i] = nums[i - 1] } // 将 num 赋给 index 处的元素 diff --git a/en/codes/swift/chapter_array_and_linkedlist/array.swift b/en/codes/swift/chapter_array_and_linkedlist/array.swift index 0a513a17c..54453f6ad 100644 --- a/en/codes/swift/chapter_array_and_linkedlist/array.swift +++ b/en/codes/swift/chapter_array_and_linkedlist/array.swift @@ -28,7 +28,7 @@ func extend(nums: [Int], enlarge: Int) -> [Int] { /* Insert element num at index index in the array */ func insert(nums: inout [Int], num: Int, index: Int) { // Move all elements at and after index index backward by one position - for i in nums.indices.dropFirst(index).reversed() { + for i in stride(from: nums.count - 1, to: index, by: -1) { nums[i] = nums[i - 1] } // Assign num to the element at index index diff --git a/ja/codes/swift/chapter_array_and_linkedlist/array.swift b/ja/codes/swift/chapter_array_and_linkedlist/array.swift index 8f9c8c209..266f6fce4 100644 --- a/ja/codes/swift/chapter_array_and_linkedlist/array.swift +++ b/ja/codes/swift/chapter_array_and_linkedlist/array.swift @@ -28,7 +28,7 @@ func extend(nums: [Int], enlarge: Int) -> [Int] { /* 配列の index 番目に要素 num を挿入 */ func insert(nums: inout [Int], num: Int, index: Int) { // インデックス index 以降の全要素を 1 つ後ろへ移動する - for i in nums.indices.dropFirst(index).reversed() { + for i in stride(from: nums.count - 1, to: index, by: -1) { nums[i] = nums[i - 1] } // index の要素に num を代入する diff --git a/ru/codes/swift/chapter_array_and_linkedlist/array.swift b/ru/codes/swift/chapter_array_and_linkedlist/array.swift index b7cca7f2c..227807284 100644 --- a/ru/codes/swift/chapter_array_and_linkedlist/array.swift +++ b/ru/codes/swift/chapter_array_and_linkedlist/array.swift @@ -28,7 +28,7 @@ func extend(nums: [Int], enlarge: Int) -> [Int] { /* Вставить элемент num по индексу index в массив */ func insert(nums: inout [Int], num: Int, index: Int) { // Сдвинуть элемент с индексом index и все последующие элементы на одну позицию назад - for i in nums.indices.dropFirst(index).reversed() { + for i in stride(from: nums.count - 1, to: index, by: -1) { nums[i] = nums[i - 1] } // Присвоить num элементу по индексу index diff --git a/zh-hant/codes/swift/chapter_array_and_linkedlist/array.swift b/zh-hant/codes/swift/chapter_array_and_linkedlist/array.swift index 356b11e3a..5fb720c31 100644 --- a/zh-hant/codes/swift/chapter_array_and_linkedlist/array.swift +++ b/zh-hant/codes/swift/chapter_array_and_linkedlist/array.swift @@ -28,7 +28,7 @@ func extend(nums: [Int], enlarge: Int) -> [Int] { /* 在陣列的索引 index 處插入元素 num */ func insert(nums: inout [Int], num: Int, index: Int) { // 把索引 index 以及之後的所有元素向後移動一位 - for i in nums.indices.dropFirst(index).reversed() { + for i in stride(from: nums.count - 1, to: index, by: -1) { nums[i] = nums[i - 1] } // 將 num 賦給 index 處的元素