Chéyo
Chéyo

Reputation: 9367

Array initializer for custom types

How can I make a custom type convertible to an array using a constructor?

 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. 

Edit: I think this may be a limitation of swift 2.1

Upvotes: 2

Views: 1249

Answers (4)

Kevin Sabbe
Kevin Sabbe

Reputation: 1452

Swift 3 example with custom Type :

Let assume that you have MyCustomType as a custom class:

class MyCustomType
{
    var name    = ""
    var code    = ""

    convenience init(name: String, code: String)
    {
        self.init()

        self.name = name
        self.code = code
    }
}

You can extend the array this way:

extension Collection where Iterator.Element == MyCustomType
{
    func getNameFrom(code: String) -> String?
    {
        for custom in self {
            if custom.code == code {
                return custom.name
            }
        }
        return nil
    }
}

usage:

let myCustomArray = [MyCustomType]()

myCustomArray.getNameFrom(code: "code")

Hope it's help! :)

Upvotes: 1

Alain T.
Alain T.

Reputation: 42143

I'm not sure why you need it to be an extension of an Array type.

Your struct (or class) could simply have a method that returns its content as an array. The syntax would be slightly different but as far as I can tell from your example, the result would be the same.

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

Upvotes: 1

user3441734
user3441734

Reputation: 17534

try look at this 'example'

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>

before you use something like this, answer by yourself the question Why not use simly

let carr = custom.array

???

Upvotes: -1

raf
raf

Reputation: 2567

How about this?:

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]

Or simplify to:

struct Custom {
    let array: [Int]
    init(_ value: Int...) {
        self.array = value
    }
}

let customArray = Custom(2, 3, 4, 5).array // [2, 3, 4, 5]

Upvotes: 0

Related Questions