2

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

4 Answers 4

1

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! :)

Sign up to request clarification or add additional context in comments.

1 Comment

Yep. Swift 3 overcame this limitations. Thanks!
1

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

Comments

0

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]

Comments

-1

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

???

4 Comments

I change the code to demonstrate a more specific case that does use an array as a field.
You are using a private _ArrayType type that I don't think is feasible for stable code.
@masters3d please, see, that i don't recommend you to use _ArrayType! I recommend you to use your .array property directly. your 'more specific case' is not so specific, you can define you array property easily. In my example i tried to show you, where is the error: same-type requirement makes generic parameter 'Element' non-generic coming from.
@masters3d by the way, _ArrayType is public protocol, but i agree, that it is intended for an 'internal' use. Swift is very new, and you can expect a lot of changes between versions in the future. prefixed with _ (underscore) or not :-). generally, what are you trying to do, is not the best idea (that is my own point of view)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.