Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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
Arrays 从2D数组中获取列– ;如何限制扩展中的数组类型?_Arrays_Swift - Fatal编程技术网

Arrays 从2D数组中获取列– ;如何限制扩展中的数组类型?

Arrays 从2D数组中获取列– ;如何限制扩展中的数组类型?,arrays,swift,Arrays,Swift,我想在Swift中扩展数组,以在二维数组的每个数组或列中返回单个元素。到目前为止,我已经: extension Array where // what goes here? func getColumn( column: Int ) -> [ Int ] { return self.map { $0[ column ] } } } 我相信我需要在where之后指定一个2D数组,但我一直无法找到正确的方法 在where后面指定2D数组的正确语法是什么? 我还

我想在Swift中扩展数组,以在二维数组的每个数组或列中返回单个元素。到目前为止,我已经:

extension Array where // what goes here?
    func getColumn( column: Int ) -> [ Int ] {
        return self.map { $0[ column ] }
    }
}
我相信我需要在
where
之后指定一个2D数组,但我一直无法找到正确的方法

where
后面指定2D数组的正确语法是什么?

我还很好奇,是否有一个好的文档说明如何指定扩展中
where
之后可用的内容。我当时找不到


提前感谢。

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

public protocol CollectionType : Indexable, SequenceType {
    // ...
    public subscript (position: Self.Index) -> Self.Generator.Element { get }
    // ...
}
因此,可以为元素为集合的数组定义扩展方法:

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

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

print(c) // [2, 5]
您甚至可以将其定义为附加的订阅方法:

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"]

更新Swift 3:

extension Array where Element : Collection {
    func getColumn(column : Element.Index) -> [ Element.Iterator.Element ] {
        return self.map { $0[ column ] }
    }
}
或作为下标:

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

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

public protocol CollectionType : Indexable, SequenceType {
    // ...
    public subscript (position: Self.Index) -> Self.Generator.Element { get }
    // ...
}
因此,可以为元素为集合的数组定义扩展方法:

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

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

print(c) // [2, 5]
您甚至可以将其定义为附加的订阅方法:

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"]

更新Swift 3:

extension Array where Element : Collection {
    func getColumn(column : Element.Index) -> [ Element.Iterator.Element ] {
        return self.map { $0[ column ] }
    }
}
或作为下标:

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

我把标题改了一点,这样其他关于检索数组列的问题可以重定向到这里,因此,有关检索数组列的其他问题可以重定向到此处。Swift 3:CollectionType替换为Collection,Generator是迭代器,因此我们有:扩展数组,其中元素:Collection{func getColumn(column:Element.Index)->[Element.Iterator.Element]{return self.map{$0[column]}}1。对于Swift4,您是否需要将
生成器
更新为
迭代器
?2.你能告诉我为什么
->Element
不工作,你需要做
->[Element.Iterator.Element]
?因为如果我使用
->Element
我会得到以下错误:无法将类型为“[Element.Element]”的返回表达式转换为返回类型“Element”@Honey:Swift 3版本也应该在Swift 4中工作。它在Xcode 9.4.1上不能为我编译,我会得到以下错误:“Array.Element”没有名为“Generator”的成员类型;你是说“发电机”吗?&““生成器”已重命名为“迭代器”,可以回答我的另一个问题well@Honey:您是否在回答的末尾使用了“针对Swift 3的更新”版本?Swift 3:CollectionType替换为Collection,Generator是迭代器,因此我们有:扩展数组,其中元素:Collection{func getColumn(column:Element.Index)->[Element.Iterator.Element]{return self.map{$0[column]}}}1.对于Swift4,您是否需要将
Generator
更新为
Iterator
?2.用外行术语,您能否告诉我
->Element
不工作的原因,以及您需要执行
->->[Element.Iterator.Element]
?因为如果我执行
->Element
操作,我会得到以下错误:无法转换'[Element.Element]类型的返回表达式'返回类型'Element'@Honey:Swift 3版本也应该在Swift 4中工作。它在Xcode 9.4.1上不能为我编译。我得到以下错误:'Array.Element'没有名为'Generator'的成员类型;你是说'Generator'?&'Generator'已重命名为'Iterator',可以回答我的另一个问题well@Honey字体你用t了吗他在回答的末尾提到了“Swift 3的更新”版本?