Arrays 自定义类型的数组初始值设定项
如何使用构造函数将自定义类型转换为数组Arrays 自定义类型的数组初始值设定项,arrays,swift,swift2,Arrays,Swift,Swift2,如何使用构造函数将自定义类型转换为数组 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){
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.
扩展数组,其中Generator.Element==Int{//相同类型要求使泛型参数'Element'非泛型
//`where Element:SignedIntegerType`可以工作,但这是一个协议
init(u值:自定义){
self=[value.item1,value.item2,value.item3,value.item4]//无法将“[Int]”类型的值分配给“Array”类型
}
}
结构自定义{
//让array=[item……]我也可以在这里有一个数组,但这不是问题的重点。
私人出租项目1:Int
私人出租项目2:Int
私人出租项目3:Int
私人出租项目4:Int
init(value1:Int,value2:Int,value3:Int,value4:Int){
self.item1=value1
self.item2=value2
self.item3=value3
self.item4=value4
}
}
让custom=Array(custom(2,3,4,5))//我希望能够转换为数组/集合。
编辑:我认为这可能是swift 2.1的一个局限性
struct Custom {
let array: [Int]
init(_ value: Int...) {
self.array = value.map({$0+1})
}
}
extension _ArrayType where Generator.Element == Int {
init(custom: Custom) {
self.init()
self.appendContentsOf(custom.array)
}
}
let custom = Custom(1,2,3)
let carr = Array(custom: custom)
print(carr, carr.dynamicType) // [2, 3, 4] Array<Int>
?这个怎么样
struct Custom{
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
}
var array : [Int] {
get {
return [self.item1, self.item2, self.item3, self.item4]
}
}
}
let customArray = Custom(2, 3, 4, 5).array // [2, 3, 4, 5]
或简化为:
struct Custom {
let array: [Int]
init(_ value: Int...) {
self.array = value
}
}
let customArray = Custom(2, 3, 4, 5).array // [2, 3, 4, 5]
我不知道为什么需要它作为数组类型的扩展 您的结构(或类)可以有一个以数组形式返回其内容的方法。语法可能略有不同,但从您的示例中我可以看出,结果是相同的
struct Custom<T>
{
private let item1:T
private let item2:T
private let item3:T
private let item4:T
init(_ value1:T, _ value2:T, _ value3:T, _ value4:T )
{
self.item1 = value1
self.item2 = value2
self.item3 = value3
self.item4 = value4
}
var array:[T] { return [item1, item2, item3, item4] }
}
let custom = Custom(2, 3, 4, 5).array
struct自定义
{
私人出租项目1:T
私人出租项目2:T
私人出租项目3:T
私人出租项目4:T
初始值(wValue1:T、wValue2:T、wValue3:T、wValue4:T)
{
self.item1=value1
self.item2=value2
self.item3=value3
self.item4=value4
}
变量数组:[T]{return[item1,item2,item3,item4]}
}
让custom=custom(2,3,4,5).array
Swift 3自定义类型示例:
假设您将MyCustomType作为自定义类:
class MyCustomType
{
var name = ""
var code = ""
convenience init(name: String, code: String)
{
self.init()
self.name = name
self.code = code
}
}
可以通过以下方式扩展阵列:
extension Collection where Iterator.Element == MyCustomType
{
func getNameFrom(code: String) -> String?
{
for custom in self {
if custom.code == code {
return custom.name
}
}
return nil
}
}
用法:
let myCustomArray = [MyCustomType]()
myCustomArray.getNameFrom(code: "code")
希望有帮助!:) 我更改代码以演示一个更具体的案例,该案例确实使用数组作为字段。您使用的是私有的
\u ArrayType
类型,我认为这对于稳定的代码是不可行的。@masters3d请看,我不建议您使用_ArrayType!我建议您直接使用.array属性。您的“更具体的情况”不是那么具体,您可以轻松定义数组属性。在我的示例中,我试图告诉您错误在哪里:同一类型要求使泛型参数'Element'不是泛型的。@masters3d顺便说一句,_ArrayType是公共协议,但我同意,它是用于“内部”用途的。Swift是一个非常新的版本,你可以期待在未来的版本之间会有很多变化。前缀为\(下划线)或非:-)。一般来说,你想做什么,不是最好的主意(这是我自己的观点)是的。Swift 3克服了这一限制。谢谢
let myCustomArray = [MyCustomType]()
myCustomArray.getNameFrom(code: "code")