从2D数组中获取列 - 如何在扩展中限制数组类型?

时间:2022-12-22 01:34:18

I'd like to extend Array in Swift to return a single element in each array, or column, for a 2D array. So far I have:

我想在Swift中扩展Array以返回2D数组的每个数组或列中的单个元素。到目前为止我有:

extension Array where // what goes here?
    func getColumn( column: Int ) -> [ Int ] {
        return self.map { $0[ column ] }
    }
}

I believe that I need to somehow specify a 2D array after where, but I have been unable to figure out the correct way to do that.

我相信我需要以某种方式指定一个2D数组,但我无法弄清楚这样做的正确方法。

What is the correct syntax for specifying a 2D array after the where?

在哪里指定2D数组的正确语法是什么?

I'm also curious if there is a good documentation for how to specify what is available for after where in an extension lives. I couldn't find that at Apple's Swift extension documentation

我也很好奇是否有一个很好的文档,说明如何指定扩展程序所在位置之后可用的内容。我在Apple的Swift扩展文档中找不到它

Thanks in advance.

提前致谢。

1 个解决方案

#1


8  

You need to constrain the Element type of the array. The subscript method is defined in the CollectionType protocol:

您需要约束数组的Element类型。下标方法在CollectionType协议中定义:

public protocol CollectionType : Indexable, SequenceType {
    // ...
    public subscript (position: Self.Index) -> Self.Generator.Element { get }
    // ...
}

Therefore you can define an extension method for arrays whose elements are collections:

因此,您可以为其元素为集合的数组定义扩展方法:

extension Array where Element : CollectionType {
    func getColumn(column : Element.Index) -> [ Element.Generator.Element ] {
        return self.map { $0[ column ] }
    }
}

Example:

例:

let a = [[1, 2, 3], [4, 5, 6]]
let c = a.getColumn(1)

print(c) // [2, 5]

You could even define it as an additional subscripting method:

您甚至可以将其定义为附加的下标方法:

extension Array where Element : CollectionType {
    subscript(column column : Element.Index) -> [ Element.Generator.Element ] {
        return map { $0[ column ] }
    }
}

let a = [["a", "b", "c"], [ "d", "e", "f" ]]
let c = a[column: 2]
print(c) // ["c", "f"]

Update for Swift 3:

Swift 3的更新:

extension Array where Element : Collection {
    subscript(column column : Element.Index) -> [ Element.Iterator.Element ] {
        return map { $0[ column ] }
    }
}

#1


8  

You need to constrain the Element type of the array. The subscript method is defined in the CollectionType protocol:

您需要约束数组的Element类型。下标方法在CollectionType协议中定义:

public protocol CollectionType : Indexable, SequenceType {
    // ...
    public subscript (position: Self.Index) -> Self.Generator.Element { get }
    // ...
}

Therefore you can define an extension method for arrays whose elements are collections:

因此,您可以为其元素为集合的数组定义扩展方法:

extension Array where Element : CollectionType {
    func getColumn(column : Element.Index) -> [ Element.Generator.Element ] {
        return self.map { $0[ column ] }
    }
}

Example:

例:

let a = [[1, 2, 3], [4, 5, 6]]
let c = a.getColumn(1)

print(c) // [2, 5]

You could even define it as an additional subscripting method:

您甚至可以将其定义为附加的下标方法:

extension Array where Element : CollectionType {
    subscript(column column : Element.Index) -> [ Element.Generator.Element ] {
        return map { $0[ column ] }
    }
}

let a = [["a", "b", "c"], [ "d", "e", "f" ]]
let c = a[column: 2]
print(c) // ["c", "f"]

Update for Swift 3:

Swift 3的更新:

extension Array where Element : Collection {
    subscript(column column : Element.Index) -> [ Element.Iterator.Element ] {
        return map { $0[ column ] }
    }
}