简体   繁体   中英

How to get data from array is struct

I had write struct looks like this:

struct MSProduct: Codable {
    let categories : [Categories]?
    let tags : [Tags]?
    let images : [Images]?
    let attributes : [Attributes]?
    let default_attributes : [String]?
    let variations : [String]?
    let grouped_products : [String]?
    let menu_order : Int?
    let meta_data : [MetaData]?
    let _links : Links?
}

struct Attributes : Codable {
    let id: Int?
    let name: String?
    let position: Int?
    let visible: Bool?
    let variation: Bool?
    let options: [String]?
}

And I want to get data from let attributes which is in struct MSProduct but this reference to other struct Attributes. Can someone tell me how can I get this data to show in tableview?

after parsing the json suppose you have stored it in product variable then,

// attributes array
let productAttributes : [Attributes]? = product.attributes

//you can use it normally like
let name = productAttributes[0].name ?? "" //default value since name is optional

//inside table row 
let name = productAttributes[indexPath.row].name ?? "" //default value since name is optional

use productAttributes array to populate the tableView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM