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

Picker Can't get the id: String of my Array Dictionary

Forums > SwiftUI

I have no problem displaying the picker. I am having difficulty in grabbing the id:String This is my JSON As I have stated that, the Picker works great, execpt I am having problems in getting the ID The error that I am getting is this.

{ "stories":
    [
        {   "id": "172",
        "event": "Floyd Lippencotte Interviews"
        }
    ]
}
Picker(selection: $selectedStory, label: Text("Picker")) {

           ForEach(stories, id: \.self) { story in
               Text(story.event)
                 .foregroundColor(Color(.lightGray))
                   .font(.body)
             }
       }
           Text("Selected day: stories \(story[selectedStory])") 

           Error *  // *Cannot find 'story' in scope*

Thank you ahead of time for giving me some insight

3      

story is the parameter to the content closure of the ForEach but you are tryiong to use it outside of that context. That's whay it can't be found.

Adjusting the indent and adding comments will hopefully show this better:

Picker(selection: $selectedStory, label: Text("Picker")) {

    ForEach(stories, id: \.self) { story in
        //story is in scope from here...
        Text(story.event)
            .foregroundColor(Color(.lightGray))
            .font(.body)
        //...to here
    }
}
//but you are trying to use story here
Text("Selected day: stories \(story[selectedStory])")

I can't say for sure without seeing the rest of your code, but you probably want to use stories[selectedStory] instead.

3      

Xcode File

That was a good guess and I did try that. If it was only the name that I could catch, that would not have

Let me explain that the item that has value for me is not the name but the id:String. And capturing it as a Environment Object named, " storyForlogging " would be the Holy Grail. For then I can go straing to Core Data to grab all the data that I need for that id:String.

RoosterBoy, Could you please send me a email from my Contact List. You can find my email on the Right Hand Side for Robert

Thank You Rooster Boy

3      

Xcode File

That was a good guess and I did try that. If it was only the name that I could catch, that would not have

Let me explain that the item that has value for me is not the name but the id:String. And capturing it as a Environment Object named, " storyForlogging "

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.