|
Hello, I'm new to Swift and ran into an error that has confused me. I have the latest versions of Xcode on my Mac and Swift Playgrounds on my iPad. I have tried this on both with the same error. Here is the code in question. It from this site and the same code Paul uses in the tutorial YouTube video. Here is the link to the tutorial: https://www.hackingwithswift.com/books/ios-swiftui/sending-and-receiving-codable-data-with-urlsession-and-swiftui
Isn't 'data' defined with the I think I must be missing something, any help would be greatly appreceated. James |
|
You have defined
so You should move your decoding line inside the |
|
Thank you. Works like the tutorial now. I'll do some reading on scopes and the do / catch. James |
|
I'm hoping something has changed since this answer was posted. I'm having exactly the same error that James was above, but I have located the decoding line inside the do {}. I've compared my code with the article, and also rewatched the video many times, but I can't see what might be wrong. I am using Xcode 14.0.1 and Swift 5.7. Her is a snippet of my code: func loadData() async { guard let url = URL(string: "https://itunes.apple.com/search?term=taylor+swift&entity=song") else { print("Invalid URL") return }
} Here's hoping you can help me! |
|
I'm hoping something has changed since this answer was posted. I'm having exactly the same error that James was above, but I have located the decoding line inside the do {}. I've compared my code with the article, and also rewatched the video many times, but I can't see what might be wrong. I am using Xcode 14.0.1 and Swift 5.7. Her is a snippet of my code: func loadData() async { guard let url = URL(string: "https://itunes.apple.com/search?term=taylor+swift&entity=song") else { print("Invalid URL") return }
} Here's hoping you can help me! |
|
Please ignore the request and <whine> above. I printed out my program and through careful scanning of the text, I discovered an extraneous and misplaced bracket. After realigning, the code works perfectly. thanks in advance for all the contributors who post to this forum. I really wish that Paul would provide a completed code snippet for each of the project videos. I spend a lot of wasted time tracking down these errors (largely to my inexperience. Is there an app or extension which can spot things like this? |
|
Unbalanced brackets are frustrating for sure. The errors are not always informative, or there may be a single root-cause error drowning in a sea of irrelevant ones. Perhaps not what you want to hear, but I consider it a fundamental skill to be able to read and recognize errors. For fun, I added an extra bracket to a random spot in the Moonshot code just to see what would happen:
As you can see there are 7 errors because of the misplaced brace and only the last one gives a clue that the problem is because of the brace. However, it's good to be familiar with the other errors. Now I know that "Type ... cannot conform to 'View'" is sometimes seen because of bracket typo. I would simply file it under the learnings you gain when typing code in by hand. And I also know to scan the whole list looking for a simple culprit even if it's the very last one. It was fun to see how much the Swift compiler would barf over a single character. :) Sometimes it's educational just to indulge your curiosity for a bit. On that note, I tend to do command-B a lot just to make sure I haven't made any silly typos. I don't think anybody codes as fast as Paul does in his videos because you have to stop a lot to think. Perfect time to do command-B and make sure things are still compiling all right. Also, Paul has uploaded the canonical versions of each project here, which is helpful sometimes in comparing problematic code. I'm not sure whether he uploaded the "technique" portions of each project though, which I think is what you are asking for. |
|
There are couple of methods to keep in mind. Double-clicking on a brace, bracket, square bracket or quotes will highlight the extent in the code of that delimiter. Also using Ctrl + I over selected lines of code will re-indent the code as necessary. Both methods could help identify missing or extraneous delimiters (brackets, braces, …). |
SPONSORED AppSweep by Guardsquare helps developers automate the mobile app security testing process with fast, free scans. By using AppSweep’s actionable recommendations, developers can improve the security posture of their apps in accordance with security standards like OWASP.
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.