TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Help appreciated on day 41.

Forums > 100 Days of SwiftUI

Hey its me again with yet another snippet of code that i dont understand here it is

init(mission: Mission, astronauts: [String: Astronaut]) {
    self.mission = mission

    self.crew = mission.crew.map { member in
        if let astronaut = astronauts[member.name] {
            return CrewMember(role: member.role, astronaut: astronaut)
        } else {
            fatalError("Missing \(member.name)")
        }
    }
}

what does map do here? also what does member stand for? isnt map for changing values?

2      

map is for taking a collection of things and transforming it into a collection of some other things.

In this case, you are taking an array of CrewRole items and transforming it into an array of CrewMember items.

member is the item being passed into the map's closure.

2      

@roosterboy thanks a lot! i presume map can also be used on singular variables and not only collections right? also what exactly would member be here? an instance of the mission struct?

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.