WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

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?

   

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.).

1      

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?

   

Hacking with Swift is sponsored by Fernando Olivares

SPONSORED Fernando's book will guide you in fixing bugs in three real, open-source, downloadable apps from the App Store. Learn applied programming fundamentals by refactoring real code from published apps. Hacking with Swift readers get a $10 discount!

Read the book

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

Reply to this topic…

You need to create an account or log in to reply.

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.