|
I'm on my project and I want to shuffle the array in my JSON file with .onAppear() but then I get an error like the one in the code below. I tried to find a solution but I just don't know what to do so if someone can help me solving this error I'd appreciate it.
this is the decode manager:
|
|
It appears that you haven't passed
becomes
And you may want to try, assuming
|
|
Thank you so much for answering! Now I've got another error, that is " 'type' is not a member type of type 'T' " on this code :
|
|
@Schoco has a hard type of problem! Type!
Function SignaturesAre you comfortable reading function signatures? If not, here's a short note I wrote to another programmer about signatures. See -> Function Signatures You have a complex signature, but you should practice reading them (out loud!) until they make sense to you. Let's take a look at your signature and see if we can decode it. It might help you understand why you're getting errors. I use two techniques.
First Bite: func decodeThis is straight forward. You've declared a Second Bite: <T:Decodable>This can be strange to understand. You have to study In short, this bit of code tells us you have declared a What does that mean? Well, you can pass in an Album Third Bite: ( _ type: T.type, xxxxxxxxxxx)The parentheses tell you that these are the parameters that you pass into the function. We're eating one bite at a time here, so focus on the first parameter. The underscore is a "don't care". When you call this function, won't need to provide a parameter name for this incoming parameter. But inside your function, you will use a variable named Fourth Bite: T.typeWhat is the Swift type of the variable named
Of course, the compiler will check that the type you specify conforms to Note!This is tricky for some new developers. Your JSON file may contain four dozen albums. What is the type you want to decode? Do you decode an Album struct? No! You are looking at an array of Album objects. So you need to declare Fifth Bite: ( xxxxxxxxxx, _ file: String)Ok, back into familiar territory. Focus on the second function parameter. Again, when calling this Final Bite: -> TRead the signature! This tells you that when this function is finished, it will return an object of the type that you previously specified. Did you specify an array of Recipes? Good luck and Keep Coding!See -> How to eat an elephant |
|
Swift should be able to infer the return type of Instead of this:
Try this:
|
|
@obelix thank you so much for your work to help me out! I don't think I have a solid understanding of generics and things like you've shown me there so I'll definitely try to get it sink in... I'll take my time ! |
|
@roosterboy thank you for your answer ! I tried your solution but I got some errors on other parts of my code so I'll try and figure it out for a while... |
|
@schocolate24 - konnichiwa , can you please try below code for decoder, see i made a minor change to parameter , so compiler better understands the type, after that look below for rest of code
|
|
@AmitShrivastava Thank you for you help :) errors are gone and my code works now ! I'll now try to understand how it actually worked ;) |
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!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.