How to fix "No exact matches in call to initializer" error in Swift
Table of Contents
While coding, you might come across this error.
No exact matches in call to initializer
It can happen in many places. Based on the message, it might not be obvious what is causing it and how to solve it.
In this article, we will learn what is causing it and how to solve it.
Cause of the problem
The "No exact matches in call to initializer" error can happen when you initialize any type, e.g., String
, ForEach
, UIColor
, Date
, etc.
The cause is the provided argument type isn't supported by any of the initializers on that type.
You can easily support sarunw.com by checking out this sponsor.
Translate your app In 1 click: Simplifies app localization and helps you reach more users.
Solutions
I can't provide a solution that covers every case since there are countless data types and arguments, but I can give you an example.
Here is an example where we get the error on SwiftUI Text
.
struct BadgeView: View {
// 1
let count: Int
var body: some View {
// 2
Text(count) // No exact matches in call to initializer
}
}
1 count
is an Int
.
2 We provide count
as an argument for a text view initializer, but no initializer accepts an Int
, hence the error.
To fix this, we need to convert an integer to one of the supported types. In this case, I convert it to a string.
There are different ways of converting an integer to a string. I can give you two.
Text(count.description)
Text("\(count)")
With these small changes, the error is gone.
Read more article about Swift, Debugging, or see all available topic
Enjoy the read?
If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.
Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.
If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.
Become a patron Buy me a coffee Tweet Share4 Picker styles in SwiftUI Form
Explore all possible picker styles, so you can choose the one that suits your needs.
How to remove Back button title in SwiftUI
Learn a proper way to remove a back button title in SwiftUI.