|
Hello, I am trying to write my first project by using the different examples from this site. But .NavigationDestination is giving me an error I don't know how to fix. The error is "Cannot convert value of type '([String], HeatControl) -> EditHeatControlView' to expected argument type '(HeatControl) -> EditHeatControlView'". I do understand that it has something to do with converting a string to something else. My code:
EditHeatControlView
|
|
What does this error even mean?
Let's first look at a simple example. Maybe you have received compiler errors like this:
This is easy to understand. You are trying to use a Next level of difficulty:
This is trickier, but carefully read the error message.
Swift is expecting you to provide an array of Next level of difficulty:
This is trickier, but carefully read the signature. Remember, in Swift Now let's look at your example in detail.
What is this type? A But what are you providing?
You're providing a function that provides two parameters: an So where in your code do you define such a function? It's not obvious, but your
Now as you pointed out, the navigation destination is the source of your problem. It's expecting a destination view, but you are providing an initializer function that returns a view.
Also considerIf your temperature variable (
Fortsätt på din väg med datorkodning (Keep coding!)For additional support about reading function signatures |
|
Thank you for helping me. I have read and partialy understood your answer so I have some homework to do.. :) I have used code from the project "iTour" found here: https://www.hackingwithswift.com/quick-start/swiftdata/swiftdata-tutorial-building-a-complete-project Can you explain to me where my code is different from the guide? I have tried to write my code as close to his. Thank you. |
|
In @twoStraw's solution for iTour his
Notice how the In your code, you define an EditHeatControlView almost, but not quite, the same:
The synthesized memberwise initializer tells Swift that when you initialize this view, you will provide
Yet, in your
The error message you received says the same thing.
You are providing this:
SwiftUI expects this:
|
|
I am glad you are taking your time to give these thorough explanations. Especialy to a newbee like me.. :) But I need you to try to explain why @twoStraw's example are working without a private variable. I tried the first part of his code and, as I can see, there is no variable defined. newSightName is used later in his example. Both adding samples and adding a new destination works. Here are the code for Contentview.
And for EditDestinationView.
|
|
If you want to make both syntaxes work, here is the code you should replace
This gives an error, as there might be another variable in the code. from this to this:
|
|
there's a type mismatch in the navigationDestination argument. You need to pass a single HeatControl object to EditHeatControlView, but the provided closure has a different signature. |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.