Missing argument for parameter 'from' in call

In the file InventoryManager when I return Inventory appears: Missing argument for parameter 'from' in call also in ContentView

InventoryManager.swift

import Foundation

class InventoryManager { static let shared = InventoryManager()

private let key = "inventory"

func saveInventory(_ inventory: Inventory) {
    if let encoded = try? JSONEncoder().encode(inventory) {
        UserDefaults.standard.set(encoded, forKey: key)
    }
}

func loadInventory() -> Inventory {
    if let data = UserDefaults.standard.data(forKey: key),
       let decoded = try? JSONDecoder().decode(Inventory.self, from: data) {
        return decoded
    }
    return Inventory() // <-- HERE
}

}

ContentView.swift

import Foundation import SwiftUI

struct ContentView: View { @State private var inventory: Inventory = Inventory() // <-- HERE

var body: some View {
    TabView {

... etc

Replies

First, please use code formatter :

// InventoryManager.swift
import Foundation
class InventoryManager {
    static let shared = InventoryManager()
    private let key = "inventory"
    
    func saveInventory(_ inventory: Inventory) {
        if let encoded = try? JSONEncoder().encode(inventory) {
            UserDefaults.standard.set(encoded, forKey: key)
        }
    }
    
    func loadInventory() -> Inventory {
        if let data = UserDefaults.standard.data(forKey: key),
           let decoded = try? JSONDecoder().decode(Inventory.self, from: data) {
            return decoded
        }
        return Inventory() // <-- HERE
    }
}

// ContentView.swift
import Foundation
import SwiftUI
struct ContentView: View {
    @State private var inventory: Inventory = Inventory() // <-- HERE
    
    var body: some View {
    TabView {

Please show how Inventory is declared.

You have probably defined an init(from:), which is thus expected at declaration.

Here is a toy example:

struct Inventory {
    var value: Int
    
    init() {
        value = 10
    }

    init(from x: Int) {
        value = x
    }
}

When you call

    @State private var inventory = Inventory()

Without init(), you get the error.