'How to write struct's property down to tableViewCell?
I have API url: https://api.dictionaryapi.dev/api/v2/entries/en/hello that returns structures:
struct WordStruct: Codable {
let word: String
let phonetic: String
let phonetics: [Phonetic]
let meanings: [Meaning]
let license: License
let sourceUrls: [String] }
struct License: Codable {
let name: String
let url: String }
struct Meaning: Codable {
let partOfSpeech: String
let definitions: [Definition] }
struct Definition: Codable {
let definition: String
let synonyms: [String]?
let example: String? }
struct Phonetic: Codable {
let text: String
let audio: String
let sourceURL: String
let license: License
enum CodingKeys: String, CodingKey {
case text, audio
case sourceURL = "sourceUrl"
case license
} }
And I have correctly decoded JSON to console but I cannot write "definition" property down to tableViewCell (see struct Definition). And here's my method:
var items = [WordStruct]()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCell
cell.definitionLalel?.text = items[indexPath.row]///the question is here...
return cell
}
Solution 1:[1]
I have not tested but I think the problem will be somewhere here. I hope you will get the idea. You have to point the meanings array, and then you can get the first element of that array. Something like this:
cell.definitionLalel?.text = items[indexPath.row].meanings[0].definitions[0].definition
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Akos Farkas |
