【发布时间】:2016-01-21 07:06:07
【问题描述】:
如何使用构造函数将自定义类型转换为数组?
extension Array where Generator.Element == Int{ // same-type requirement makes generic parameter 'Element' non-generic
// `where Element: SignedIntegerType` would work but that is a protocol
init(_ value:Custom){
self = [value.item1, value.item2, value.item3, value.item4 ] // cannot assign value of type '[Int]' to type 'Array<_>'
}
}
struct Custom{
// let array = [item.........] I could also have an array here but that is not the point of the question.
private let item1:Int
private let item2:Int
private let item3:Int
private let item4:Int
init(_ value1:Int, _ value2:Int, _ value3:Int, _ value4:Int ){
self.item1 = value1
self.item2 = value2
self.item3 = value3
self.item4 = value4
}
}
let custom = Array(Custom(2, 3, 4, 5))// I want to be be able to convert to an array/set.
编辑:我认为这可能是 swift 2.1 的限制
【问题讨论】: