自定义类型的数组初始化程序

时间:2016-01-21 07:06:07

标签: 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){
            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的限制

4 个答案:

答案 0 :(得分:1)

我不确定为什么你需要它作为数组类型的扩展。

您的struct(或类)可以只使用一个方法将其内容作为数组返回。语法略有不同,但就我的例子而言,结果将是相同的。

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

答案 1 :(得分:1)

使用自定义类型的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")

希望它有所帮助! :)

答案 2 :(得分:0)

这个怎么样?:

ListView

或简化为:

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]

答案 3 :(得分:-1)

试着看看这个&#39;示例&#39;

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>
在您使用之前

回答这个问题 为什么不使用simly

let carr = custom.array

<强> ???