GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

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 try! Swift Tokyo.

SPONSORED Ready to dive into the world of Swift? try! Swift Tokyo is the premier iOS developer conference will be happened in April 9th-11th, where you can learn from industry experts, connect with fellow developers, and explore the latest in Swift and iOS development. Don’t miss out on this opportunity to level up your skills and be part of the Swift community!

Get your ticket 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.