Swift struct error: missing argument labels in call
The Swift compiler reports missing argument labels in call when the labels used at a function, method, or initializer call do not match its declaration. With a Swift structure, this commonly happens when an initializer expects labels such as brand:, name:, and capacity:, but the values are passed without those labels.
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
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:
Why Swift Requires the struct Initializer Labels
The initializer is declared as init(brand:name:capacity:). Unless an underscore is used in the declaration, Swift treats each parameter name as an external argument label as well as a local parameter name.
The call must therefore match the initializer signature:
car(brand: value, name: value, capacity: value)
The compiler diagnostic also lists the missing labels. In this example, it explicitly requests brand:name:capacity:.
Fix the Missing Argument Labels in the Swift struct Call
To solve this error, we have to provide the argument labels while calling the structure with initial values.
main.swift
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
Remove Swift Initializer Argument Labels with an Underscore
If an unlabeled call is intentional, declare an underscore before each initializer parameter. The underscore means that callers should pass the corresponding value without an external argument label.
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(car1.name)
Output
Creta
Use this form only when the meaning and order of the arguments remain clear at the call site. Labels usually make calls with multiple values easier to read.
Use Custom Swift Argument Labels Without Changing Property Names
Swift also allows an initializer parameter to have a separate external label and local name. The first name is used by the caller, and the second name is used inside the initializer.
struct Car {
var brand: String
var name: String
init(from brand: String, model name: String) {
self.brand = brand
self.name = name
}
}
let car = Car(from: "Hyundai", model: "Creta")
print("\(car.brand) \(car.name)")
Output
Hyundai Creta
In from brand: String, from is the external argument label and brand is the local parameter name.
Check Argument Labels, Order, and Initializer Overloads
If adding labels does not fix the error, compare the complete call with the available initializer declarations.
- Confirm that every required argument label is present.
- Keep arguments in the order defined by the initializer.
- Check spelling and letter case because Swift identifiers are case-sensitive.
- Verify that the argument types match the parameter types.
- Check whether a different initializer overload expects a different set of labels.
- Use an underscore only when the initializer declaration intentionally omits a label.
Swift Memberwise Initializer and Missing Argument Labels
A structure without a custom initializer may receive a memberwise initializer for its stored properties. That generated initializer normally uses the property names as argument labels.
struct User {
var name: String
var age: Int
}
let user = User(name: "Maya", age: 28)
print(user.name)
Calling it as User("Maya", 28) produces a missing argument labels error because the generated initializer expects name: and age:.
Frequently Asked Questions About Missing Argument Labels in Swift
What does missing argument labels in call mean in Swift?
It means the function, method, or initializer declaration expects external argument labels that are absent or incorrect at the call site.
How do I find the required labels for a Swift initializer?
Read the initializer declaration or the compiler diagnostic. For init(brand:name:capacity:), the call must normally include brand:, name:, and capacity:.
How do I call a Swift struct initializer without labels?
Place an underscore before the parameter’s local name in the declaration, such as init(_ name: String). The call can then use TypeName("value").
Why does a Swift memberwise initializer require property names?
The automatically generated memberwise initializer normally uses stored-property names as argument labels, so those labels must be included when creating the value.
Swift Missing Argument Labels Resolution
In this Swift Tutorial, we learned that the call syntax must match the initializer’s external argument labels. Add the labels shown in the declaration or compiler message, or explicitly use underscores in the initializer when unlabeled arguments are appropriate.
TutorialKart.com