|
I would like to know why the last code line in AirlineView (TravelView...) gives me the error "Cannot convert value of type 'Airline' to expected argument type 'TravelView'." Also, what woiuld be a way of doing this?
|
|
Hi
|
|
|
|
The error "Cannot convert value of type 'Airline' to expected argument type 'TravelView'" happens because in AirlineView, you're trying to call TravelView.addAirline(newAirline). But addAirline is an instance method of TravelView, meaning you need an instance of TravelView to call it (you can’t call it on TravelView directly as if it were a static method). Additionally, TravelView is not available within AirlineView. To fix this, you’ll need to make AirlineView able to modify TravelView's list of airlines directly. One way to do this is to use @Binding in AirlineView so that it can directly update the airlines array defined in TravelView. Here’s how you can set it up: Solution Steps Use @Binding in AirlineView to allow it to modify TravelView's airlines array. Move the addAirline method into AirlineView. Pass airlines from TravelView to AirlineView as a binding. Here’s the updated code:
|
SPONSORED Transform your career with the iOS Lead Essentials. This Black Friday, unlock over 40 hours of expert training, mentorship, and community support to secure your place among the best devs. Click for early access to this limited offer and a free crash course.
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.