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

Why does an argument need to be a View Type?

Forums > SwiftUI

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?

import SwiftUI

struct Airline: Identifiable {
    var id = UUID()
    var airline: String
    var member: String
    var phone: String
}

struct TravelView: View {
    @State private var airlines: [Airline] = []

    var body: some View {
        VStack {
            List {
                ForEach(airlines, id: \.id) { line in
                    Text(line.airline)
                    Text(line.member)
                    Text(line.phone)
                }
            }
        }
    }

    func addAirline(newAirline: Airline) {
        airlines.append(newAirline)
    }
}

struct AirlineView: View {
    @State private var airlineName = ""
    @State private var memberNbr = ""
    @State private var phoneNbr = ""

    var body: some View {
        Form {
            TextField("Name of airline", text: $airlineName)
            TextField("Member number", text: $memberNbr)
            TextField("Phone number", text: $phoneNbr)
                .onSubmit {
                    var newAirline = Airline(airline: airlineName, member: memberNbr, phone: phoneNbr)
                    TravelView.addAirline(newAirline)
                }
        }
    }
}

#Preview {
    TravelView()
}

   

Hi @NevadaCityBob

  1. Add @Binding var airlines: [Airline] to AirlineView.
  2. Move the method addAirline(newAirline: ) to AirlineView.
  3. Delete TravelView. from the call of addAirline(newAirline: ).

    struct AirlineView: View {
    @Binding var airlines: [Airline]
    @State private var airlineName = ""
    @State private var memberNbr = ""
    @State private var phoneNbr = ""
    
      var body: some View {
          Form {
              TextField("Name of airline", text: $airlineName)
              TextField("Member number", text: $memberNbr)
              TextField("Phone number", text: $phoneNbr)
                  .onSubmit {
                      let newAirline = Airline(airline: airlineName, member: memberNbr, phone: phoneNbr)
                      addAirline(newAirline: newAirline)
                  }
          }
      }
    
      func addAirline(newAirline: Airline) {
          airlines.append(newAirline)
      }
    }

   

Thanks @NigelGee, works like a charm.

   

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:

import SwiftUI

struct Airline: Identifiable {
    var id = UUID()
    var airline: String
    var member: String
    var phone: String
}

struct TravelView: View {
    @State private var airlines: [Airline] = []

    var body: some View {
        VStack {
            List {
                ForEach(airlines, id: \.id) { line in
                    Text(line.airline)
                    Text(line.member)
                    Text(line.phone)
                }
            }

            AirlineView(airlines: $airlines) // Pass `airlines` as a binding to `AirlineView`
        }
    }
}

struct AirlineView: View {
    @Binding var airlines: [Airline]  // Use @Binding to modify `airlines` from TravelView
    @State private var airlineName = ""
    @State private var memberNbr = ""
    @State private var phoneNbr = ""

    var body: some View {
        Form {
            TextField("Name of airline", text: $airlineName)
            TextField("Member number", text: $memberNbr)
            TextField("Phone number", text: $phoneNbr)
                .onSubmit {
                    let newAirline = Airline(airline: airlineName, member: memberNbr, phone: phoneNbr)
                    addAirline(newAirline: newAirline)
                }
        }
    }

    // Move `addAirline` here to modify the `airlines` array directly
    func addAirline(newAirline: Airline) {
        airlines.append(newAirline)
    }
}

   

Hacking with Swift is sponsored by Essential Developer.

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.

Save your spot

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

Reply to this topic…

You need to create an account or log in to reply.

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.