Reputation: 649
I have an array of struct containing several fields. I want to map the array to create another one of struct containing a subset of fields
struct History: {
let field1: String
let field2: String
let field3: String
let field4: String
}
struct SubHistory: {
let field1: String
let field2: String
}
I could use the for in loop. But It may be possible to use a map, no ?
Upvotes: 2
Views: 2558
Reputation: 17471
Yes, whenever you have an Array
, or any type implementing the Collection
protocol, where map
is defined, you can use map
to transform [A]
into [B]
.
kiwisip already gave a good answer, and Martin R suggested a nice variation.
Here's another one.
extension History {
var subHistory: SubHistory {
return SubHistory(field1: field1, field2: field2)
}
}
// use it like
let histories: [History] = [...]
let subHistories = histories.map { $0.subHistory }
Upvotes: 3
Reputation: 539765
As a slight variation of kiwisip's correct answer: I would suggest to put the logic of “creating a SubHistory from a History” into a custom initializer:
extension SubHistory {
init(history: History) {
self.init(field1: history.field1, field2: history.field2)
}
}
Then the mapping can be simply done as
let histories: [History] = ...
let subHistories = histories.map(SubHistory.init)
Putting the initializer into an extension has the advantage that the default memberwise initializer is still synthesized – attribution for this observation goes to @kiwisip!
Upvotes: 5
Reputation: 447
Yes, you can use map
in the following way:
let histories: [History] // array of History structs
let subHistories = histories.map { SubHistory(field1: $0.field1, field2: $0.field2) }
Upvotes: 6