Ios 将附加数组';数组中的对象是否每次都会保持对象排序顺序?

Ios 将附加数组';数组中的对象是否每次都会保持对象排序顺序?,ios,arrays,swift,Ios,Arrays,Swift,我正在做一些操作,比如排序、过滤和按数组对象的一些属性分组 我将过滤数组的对象添加到另一个数组中,如: arrGroup.append(contentsOf: filteredArray) 我的问题是:是否所有对象每次都会在数组中保持相同的排序顺序,并且100%确定 从逻辑上讲,它会像这样添加对象吗 for object in filteredArray { arrGroup.append(object) } 或 for index in 0...filteredArray.cou

我正在做一些操作,比如排序、过滤和按数组对象的一些属性分组

我将过滤数组的对象添加到另一个数组中,如:

arrGroup.append(contentsOf: filteredArray)
我的问题是:是否所有对象每次都会在数组中保持相同的排序顺序,并且100%确定

从逻辑上讲,它会像这样添加对象吗

for object in filteredArray {
    arrGroup.append(object) 
}

for index in 0...filteredArray.count {
    let object = filteredArray[index]
    arrGroup.append(object)
}
对我来说,所有这些都是相同的,只是运行时CPU周期的不同。但我的朋友说我应该选择最后一个选项。从技术上讲,每次调试代码时,我都会得到相同的结果


请给出您的建议。

您上面的两个for循环正在做同样的事情,它们将从对象0迭代到最后一个对象

第一种方法称为
快速枚举
,因此比第二种方法更快、更有效


回答你的问题。是的,顺序将保持不变。

您上面的两个for循环正在做相同的事情,它们将从对象0迭代到最后一个对象

第一种方法称为
快速枚举
,因此比第二种方法更快、更有效


回答你的问题。是的,顺序将保持不变。

Array
保留您给出的任何顺序

Array.append(contentsOf:)
按顺序将第二个数组的所有项追加到第一个数组的末尾。下面是该算法的大致情况:

extension Array {
    mutating func myAppend(contentsOf other: [Element]) {
        reserveCapacity(self.count + other.count)

        for element in other {
            self.append(element)
        }
    }
}
迭代数组的技术 如果你只需要元素 首选方法 迭代
序列
项的首选方法是使用典型的
for in
循环:

for element in array { // most preferred!
    // use the element
}
气馁的方法 0中的i的
我强烈反对这种技术。原因是很容易成为一个错误的牺牲品。事实上,你自己的例子就是这样

for index in 0...filteredArray.count {
    let object = filteredArray[index] // when index is filteredArray.count ... Yes, when you add an Array to another Array it will maintain the order as it is.
But yes if you are using Set then you might not get same order as it is not ordered collection but Array is ordered collection which maintains it's ordering until you changes it manually.

here is the code example :

var arr1 = ["1", "2" , "3"]     // Print arr1 : ["1", "2", "3"]
let arr2 = ["4", "5" , "6"]     // Print arr2 : ["4", "5", "6"]

arr1.append(contentsOf: arr2)   // Print arr1 : ["1", "2", "3", "4", "5", "6"]
用于0中的索引…filteredArray.count{

让object=filteredArray[index]//当index为filteredArray.count时…
Array
保留您给定的任何顺序

Array.append(contentsOf:)
将第二个数组的所有项按顺序追加到第一个数组的末尾。该算法大致如下:

extension Array {
    mutating func myAppend(contentsOf other: [Element]) {
        reserveCapacity(self.count + other.count)

        for element in other {
            self.append(element)
        }
    }
}
迭代数组的技术 如果你只需要元素 首选方法 迭代
序列
项的首选方法是使用典型的
for in
循环:

for element in array { // most preferred!
    // use the element
}
气馁的方法 0中的i的
我强烈反对这种技术。原因是它很容易成为一个错误的牺牲品。事实上,你自己的例子就是这样

for index in 0...filteredArray.count {
    let object = filteredArray[index] // when index is filteredArray.count ... Yes, when you add an Array to another Array it will maintain the order as it is.
But yes if you are using Set then you might not get same order as it is not ordered collection but Array is ordered collection which maintains it's ordering until you changes it manually.

here is the code example :

var arr1 = ["1", "2" , "3"]     // Print arr1 : ["1", "2", "3"]
let arr2 = ["4", "5" , "6"]     // Print arr2 : ["4", "5", "6"]

arr1.append(contentsOf: arr2)   // Print arr1 : ["1", "2", "3", "4", "5", "6"]
用于0中的索引…filteredArray.count{

让object=filteredArray[index]//当索引为filteredArray.count时…,当您将一个数组添加到另一个数组时,它将保持原样的顺序。 但如果您使用的是Set,则可能无法获得相同的顺序,因为它不是有序集合,但数组是有序集合,在您手动更改它之前,它会保持其顺序

下面是代码示例:


,当您将一个数组添加到另一个数组时,它将保持原样。 但如果您使用的是Set,则可能无法获得相同的顺序,因为它不是有序集合,但数组是有序集合,在您手动更改它之前,它会保持其顺序

下面是代码示例:


由于它是数组,我相信arrGroup.append(contentsOf:filteredArray)总是以与filteredArray相同的顺序追加。这应该在查看代码时询问。好的,第二个代码段会因为试图访问
filteredArray[filteredArray.count]
而导致崩溃,所以绝对不要听朋友的话。@Alexander,然后是arrGroup.append(contentsOf:filteredArray)是最好的方法吗?因为它是数组,我相信arrGroup.append(contentsOf:filteredArray)总是以与filteredArray相同的顺序追加。这应该在查看代码时询问。第二个代码段会导致崩溃,因为它试图访问
filteredArray[filteredArray.count]
,所以绝对不要听你朋友的话。@Alexander,然后是arrGroup.append(内容:filteredArray)最好的方法是正确的吗?第一个代码段没有比另一个更快的地方。它们在幕后工作相同。@Alexander不,它们不完全相同。之所以称为快速枚举是有原因的。请检查这个与Swift无关的答案。谢谢你的帮助,我想现在#1是最好的方法。没有任何错误与另一个代码段不同,第一个代码段的工作方式相同。@Alexander不,它们不相同。之所以称为快速枚举是有原因的。请查看与Swift无关的答案。感谢您的帮助,我想现在#1是最好的方式。