Swift struct error: missing argument labels in call

The struct error: missing argument labels in call can occur for a call to the structure.

This type of error occurs when you create an object for a struct, but did not mention argument labels in the struct call.

Let us recreate this scenario.

main.swift

</>
Copy
struct car {
   var brand: String
   var name: String
   var capacity: Int
   
   init(brand: String, name: String, capacity: Int) {
      self.brand = brand
      self.name = name
      self.capacity = capacity
   }
}

let car1 = car("Hyundai", "Creta", 5)

print("Car details\n-----------")
print("Brand is \(car1.brand)")
print("Name is \(car1.name)")
print("Seating capacity is \(car1.capacity)")

Output

main.swift:13:15: error: missing argument labels 'brand:name:capacity:' in call
let car1 = car("Hyundai", "Creta", 5)
              ^
               brand:    name:    capacity:

To solve this error, we have to provide the argument labels while calling the structure with initial values.

main.swift

</>
Copy
struct car {
   var brand: String
   var name: String
   var capacity: Int
   
   init(brand: String, name: String, capacity: Int) {
      self.brand = brand
      self.name = name
      self.capacity = capacity
   }
}

let car1 = car(brand: "Hyundai", name: "Creta", capacity: 5)

print("Car details\n-----------")
print("Brand is \(car1.brand)")
print("Name is \(car1.name)")
print("Seating capacity is \(car1.capacity)")

Observe that we have provided the argument labels: brand, name and capacity in the struct call.

Now the program runs without any error.

Output

Car details
-----------
Brand is Hyundai
Name is Creta
Seating capacity is 5

Conclusion

In this Swift Tutorial, we have learned to solve the error: missing argument labels in call.