Anton Anton - 5 months ago 29
Swift Question

Swift array struct

Consider:

struct abc {
let def: Double
}

func xyz(data : [Double]) {
...
}

var x: [abc]


I want to call
func xyz
with the
def
value first 6 elements of
x
.

I tried:

xyz(x[0...5].def)


How do I do this?

Rob Rob
Answer

If you want to build an array of the def properties for the first six x items, you can use map:

let y = x[0...5].map { (object: abc) -> (Double) in
    return object.def 
}
xyz(y)

Or you can condense that to:

xyz(x[0...5].map { $0.def })