Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 向CollectionType(数组)添加排序函数_Swift_Swift2 - Fatal编程技术网

Swift 向CollectionType(数组)添加排序函数

Swift 向CollectionType(数组)添加排序函数,swift,swift2,Swift,Swift2,我想将自己的排序函数添加到数组中,因此我尝试扩展CollectionType协议以添加此功能。 这就是我到目前为止所做的: extension CollectionType where Generator.Element : Comparable, Index : IntegerType{ func psoInsertionSort(){ var key: Generator.Element var x, y: Int for (x

我想将自己的排序函数添加到数组中,因此我尝试扩展CollectionType协议以添加此功能。 这就是我到目前为止所做的:

extension CollectionType where Generator.Element : Comparable, Index : IntegerType{
    func psoInsertionSort(){

        var key: Generator.Element
        var x, y: Int


        for (x = 0; x < self.count; x++){
            key = self[x]

            for (y = x; y >= 0; y--){
                if key < self[y]{
                    self.removeAtIndex(y+1)
                    self.insert(key, atIndex: y)
                }
            }
        }
    }
}
扩展集合类型,其中生成器。元素:可比较,索引:IntegerType{
func psoInsertionSort(){
var键:生成器。元素
变量x,y:Int
对于(x=0;x=0;y--){
如果键
我需要将CollectionType中的元素约束为Compariable,以便进行实际排序,我相信这没有问题

我遇到的问题是for循环参数:

对于(x=0;x
二进制运算符“您只需将它们添加为协议要求。因此,您可以要求索引距离为
Int

Index.Distance == Int 
或者您可以更改循环条件:

for (var x = self.startIndex; x < self.endIndex; x++){
此外,函数
removeatinex
insert
不仅需要
CollectionType
,还需要
rangereplacablecollectiontype

并且该函数需要标记为
mutating

for x in self.indices