UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Project 14 - "pageid" vs. "pageID"

Forums > 100 Days of SwiftUI

I posted a few days ago about trying to fix my code for the BucketList app, because it constantly sent me to the .failed state. After figuring out that it wasn't the URL (although I'm pretty sure that the one that appears at http://bit.ly/swiftwiki is incorrect), I eventually figured out it was how I was labeling one of my properties (pageid works; pageID does not).

struct EditView: View {
...
                Section("Nearby...") {
                    switch loadingState {
                        case .loaded:
                            ForEach(pages, id: \.pageid) { page in
                                Text(page.title)
                                    .font(.headline)
                                + Text(": ")
                                + Text("Page description here")
                                    .italic()
                            }
                        case .loading:
                            Text("Loading...")
                        case .failed:
                            Text("Please try again later.")
                    }
                }
...
}
struct Page: Codable {
    let pageid: Int
    let title: String
    let terms: [String: [String]]?
}

Why does using "pageID" in both locations lead to .failed whereas "pageid," leads to .loaded?

2      

Sorry for the lack of clarity with my earlier post--I tried to save some reading space by taking a few shortcuts.

The code in the first post works, whereas the following code does not:

struct EditView: View {
...
                Section("Nearby...") {
                    switch loadingState {
                        case .loaded:
                            ForEach(pages, id: \.pageID) { page in
                                Text(page.title)
                                    .font(.headline)
                                + Text(": ")
                                + Text("Page description here")
                                    .italic()
                            }
                        case .loading:
                            Text("Loading...")
                        case .failed:
                            Text("Please try again later.")
                    }
                }
...
}
struct Page: Codable {
    let pageID: Int
    let title: String
    let terms: [String: [String]]?
}

The only difference in the code between versions is how the property is named and referred to: pageid vs. pageID. I don't understand why the capital letters make a difference in how the app acts. I can't find pageID in any Swift documentation, so it doesn't appear to be "owned" by Swift (like struct, extension, Result, etc.).

3      

JSON keys (and Swift identifier names) are case-sensitive, so pageid and pageID and pageId are not the same thing.

Now, look at the JSON that the API returns: Wikimedia API:Geosearch. What does the page's id key look like?

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

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.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.