JSON 데이터를 구문 분석하고 Question에 할당 한 후 결과의 개별 구성원을 인쇄하려고합니다.구조체의 개별 값에 액세스하는 방법?
print(question.results)
하지만 얻을 :
[Trivia_Game.Results(category: "Entertainment: Music", question: "Which of the following bands is Tom DeLonge not a part of?", correct_answer: "+44", incorrect_answers: ["Box Car Racer", "Blink-182", "Angels & Airwaves"])]
가 어떻게 같은 "카테고리"또는 "질문"으로 개별 멤버에 액세스 할
struct Question: Decodable
{
let response_code: Int
let results: [Results]
}
struct Results: Decodable
{
let category: String
let question: String
let correct_answer: String
let incorrect_answers: [String]
}
나는 사용하여 시도?
고맙습니다. 빠른 후속 질문, URLSession.shared.dataTask 외부의 요소에 액세스하려고합니다 (with : url). 변수를 외부에 생성했지만 전달하려고 시도했지만 작동하지 않습니다. – Keith
스위프트 (Swift)에서 JSON을 사용하는 데 대한 지식이 거의 없지만, 당신이 의미하는 바를 정확히 이해하고 있는지 확신 할 수 없으므로, 물어 보는 것이 가장 좋은 사람이 아닐 수도 있습니다. 아마이 링크들 중 하나가 도움이 될 것입니다 : https://developer.apple.com/documentation/foundation/urlsession/1411554-datatask 또는 https://developer.apple.com/documentation/foundation/urlsession – liamnickell
감사합니다 ! 그것을 알아 냈다. – Keith