swiftswiftuidependency-injectionviewswinject

error Extra arguments at positions #1, #2 in call


Hello I'm in a swiftUI project with the mvvm format I want to get the client sessions with a dependency but I have the following error Extra arguments at positions #1, #2 in call I have a view a viewmodel and an AppSettingsModules thank you for your help

here is the viewmodel

class VideoQualityViewModel: ObservableObject {
    @Published var allOptions: [VideoQualityOption] = VideoQualityEnum.allCases.map { $0.toVideoQualityOption() }
    @Published var availableVideoQualityOptions: [VideoQualityOption] = []
    @Published var selectedVideoQualityOption: VideoQualityOption?

    private let sessionClient: SessionClient

    init(dependency: Container) {
        self.sessionClient = dependency.resolve(SessionClient.self).unwrapOrFatal()
        configureOptions()
        setDefaultOption()
    }

    private func configureOptions() {
        // Configure the available video quality options based on the user's rights
        let userRight = sessionClient.currentAppSessionState.userRight

        switch userRight {
        case .short, .basic:
            // Users with basic or short rights cannot access "Full HD"
            availableVideoQualityOptions = allOptions.filter { $0.id != VideoQualityEnum.fullHD.name }
        case .max:
            // Users with max rights have access to all options
            availableVideoQualityOptions = allOptions
        }
    }

    private func setDefaultOption() {
        selectedVideoQualityOption = availableVideoQualityOptions.first { $0.id == VideoQualityEnum.automatique.name }
    }

    func saveSelectedQuality() {
        // Saves the selected option and prints its name
        guard let selectedOption = selectedVideoQualityOption else { return }
        print("Saved video quality: \(selectedOption.name)")
    }
}

    func videoQualityPresentable() -> Presentable {
        let sessionClient = dependencyContainer.resolve(SessionClient.self).unwrapOrFatal("SessionClient not injected")
        let viewModel = VideoQualityViewModel(dependencyContainer: dependencyContainer, sessionClient: sessionClient)
        let viewController = UIHostingController(rootView: VideoQualityView(videoQualityViewModel: viewModel))
        viewController.title = "Qualité Vidéo"
        return createPresentable(viewController: viewController)
    }

thank you for your help


Solution

  • As others have said in comments, your VideoQualityViewModel class only has one initializer, init(dependency: Container). That initializer only takes one parameter. You can't call an initializer for VideoQualityViewModel that takes 2 parameters unlesss you create such an initializer.

    The correct solution is to either change change your initializer to take both parameters, or to add a 2nd initializer that takes 2 parameters:

    init(dependency: Container, sessionClient: SessionClient) {
        self.sessionClient = sessionClient
        configureOptions()
        setDefaultOption()
    }
    

    Simply adding that as a second initializer would probably solve your problem.

    Note that this is fundamental Swift programming. You might want to stop and study the basics of the language before continuing, or you are going to struggle.