将数据模型从一个视图控制器传递到另一视图控制器

时间:2020-06-26 07:38:12

标签: json uicollectionview parameter-passing

我有一个数据json响应,如下所示:

{
    "$id": "1",
    "message": "Successfull",
    "mountingList": [
        {
            "$id": "2",
            "settingId": 2683991,
            "vendorId": "2906",
            "name": "ENGAGEMENT RINGS FANCY SHAPE BAGUETTE REMOUNTS",
            "imageUrl": "https://www.gemfind.net/jewelryimages/2906/media/productimages/82754.jpg",
            "cost": 2449.0,
            "centerStoneMinCarat": "0.86",
            "centerStoneMaxCarat": "1.14",
            "originalCost": "",
            "mainImageURL": "https://www.gemfind.net/jewelryimages/2906/media/productimages/82754.jpg",
            "roundImageURL": "",
            "asscherImageURL": "",
            "emeraldImageURL": "",
            "radiantImageURL": "",
            "cushionImageURL": "",
            "marquiseImageURL": "",
            "ovalImageURL": "",
            "heartImageURL": "",
            "pearImageURL": "",
            "princessImageURL": "",
            "dealerId": "2803",
            "stockNumber": "82754",
            "isFavorite": false,
            "favId": null,
            "designerLogo": "https://www.gemfind.net/ResizeLogo.aspx?img=http://gemfind.net/dealerid_display/dealerimages/886660384logo_500x500_Wbg.png&wi=200&hi=200",
            "currencyFrom": "USD",
            "currencySymbol": "US$",
            "metalTypes": [
                {
                    "$id": "3",
                    "metalType": "14K Rose Gold"
                },
                {
                    "$id": "4",
                    "metalType": "14K White Gold"
                },
                {
                    "$id": "5",
                    "metalType": "14K Yellow Gold"
                },
                {
                    "$id": "6",
                    "metalType": "18K Rose Gold"
                },
                {
                    "$id": "7",
                    "metalType": "18K White Gold"
                },
                {
                    "$id": "8",
                    "metalType": "18K Yellow Gold"
                },
                {
                    "$id": "9",
                    "metalType": "Platinum"
                }
            ],
            "collections": [
                {
                    "$id": "10",
                    "collectionName": "Fancy Shape"
                }
            ],
            "allowableDiamondShapes": [
                {
                    "$id": "11",
                    "allowableDiamondShape": "Asscher"
                },
                {
                    "$id": "12",
                    "allowableDiamondShape": "Round"
                },
                {
                    "$id": "13",
                    "allowableDiamondShape": "Emerald"
                },
                {
                    "$id": "14",
                    "allowableDiamondShape": "Heart"
                },
                {
                    "$id": "15",
                    "allowableDiamondShape": "Marquise"
                },
                {
                    "$id": "16",
                    "allowableDiamondShape": "Oval"
                },
                {
                    "$id": "17",
                    "allowableDiamondShape": "Pear"
                },
                {
                    "$id": "18",
                    "allowableDiamondShape": "Princess"
                },
                {
                    "$id": "19",
                    "allowableDiamondShape": "Radiant"
                },
                {
                    "$id": "20",
                    "allowableDiamondShape": "Round"
                }
            ],
            "priceSettingId": 2687392,
            "videoURL": "https://www.overnightmountings.com/gemfind/library/Download_video_files_for_individual_styles/82754/82754.video.white.mp4",
            "showPrice": true
        },
        {
            "$id": "21",
            "settingId": 2684026,
            "vendorId": "2906",
            "name": "ENGAGEMENT RINGS FANCY SHAPE BAGUETTE REMOUNTS",
            "imageUrl": "https://www.gemfind.net/jewelryimages/2906/media/productimages/81885.jpg",
            "cost": 1287.0,
            "centerStoneMinCarat": "0.45",
            "centerStoneMaxCarat": "1.14",
            "originalCost": "",
            "mainImageURL": "https://www.gemfind.net/jewelryimages/2906/media/productimages/81885.jpg",
            "roundImageURL": "",
            "asscherImageURL": "",
            "emeraldImageURL": "",
            "radiantImageURL": "",
            "cushionImageURL": "",
            "marquiseImageURL": "",
            "ovalImageURL": "",
            "heartImageURL": "",
            "pearImageURL": "",
            "princessImageURL": "",
            "dealerId": "2803",
            "stockNumber": "81885",
            "isFavorite": false,
            "favId": null,
            "designerLogo": "https://www.gemfind.net/ResizeLogo.aspx?img=http://gemfind.net/dealerid_display/dealerimages/886660384logo_500x500_Wbg.png&wi=200&hi=200",
            "currencyFrom": "USD",
            "currencySymbol": "US$",
            "metalTypes": [
                {
                    "$id": "22",
                    "metalType": "14K Rose Gold"
                },
                {
                    "$id": "23",
                    "metalType": "14K White Gold"
                },
                {
                    "$id": "24",
                    "metalType": "14K Yellow Gold"
                },
                {
                    "$id": "25",
                    "metalType": "18K Rose Gold"
                },
                {
                    "$id": "26",
                    "metalType": "18K White Gold"
                },
                {
                    "$id": "27",
                    "metalType": "18K Yellow Gold"
                },
                {
                    "$id": "28",
                    "metalType": "Platinum"
                }
            ],
            "collections": [
                {
                    "$id": "29",
                    "collectionName": "Fancy Shape"
                }
            ],
            "allowableDiamondShapes": [
                {
                    "$id": "30",
                    "allowableDiamondShape": "Asscher"
                },
                {
                    "$id": "31",
                    "allowableDiamondShape": "Round"
                },
                {
                    "$id": "32",
                    "allowableDiamondShape": "Emerald"
                },
                {
                    "$id": "33",
                    "allowableDiamondShape": "Heart"
                },
                {
                    "$id": "34",
                    "allowableDiamondShape": "Marquise"
                },
                {
                    "$id": "35",
                    "allowableDiamondShape": "Oval"
                },
                {
                    "$id": "36",
                    "allowableDiamondShape": "Pear"
                },
                {
                    "$id": "37",
                    "allowableDiamondShape": "Princess"
                },
                {
                    "$id": "38",
                    "allowableDiamondShape": "Radiant"
                },
                {
                    "$id": "39",
                    "allowableDiamondShape": "Round"
                }
            ],
            "priceSettingId": 2688646,
            "videoURL": "https://www.overnightmountings.com/gemfind/library/Download_video_files_for_individual_styles/81885/81885.video.white.mp4",
            "showPrice": true
        },
        {
            "$id": "40",
            "settingId": 2693143,
            "vendorId": "2906",
            "name": "3 STONE RING WITH MQ SIDE STONES",
            "imageUrl": "https://www.gemfind.net/jewelryimages/2906/media/productimages/85134.jpg",
            "cost": 993.0,
            "centerStoneMinCarat": "0.86",
            "centerStoneMaxCarat": "1.14",
            "originalCost": "",
            "mainImageURL": "https://www.gemfind.net/jewelryimages/2906/media/productimages/85134.jpg",
            "roundImageURL": "",
            "asscherImageURL": "",
            "emeraldImageURL": "",
            "radiantImageURL": "",
            "cushionImageURL": "",
            "marquiseImageURL": "",
            "ovalImageURL": "",
            "heartImageURL": "",
            "pearImageURL": "",
            "princessImageURL": "",
            "dealerId": "2803",
            "stockNumber": "85134",
            "isFavorite": false,
            "favId": null,
            "designerLogo": "https://www.gemfind.net/ResizeLogo.aspx?img=http://gemfind.net/dealerid_display/dealerimages/886660384logo_500x500_Wbg.png&wi=200&hi=200",
            "currencyFrom": "USD",
            "currencySymbol": "US$",
            "metalTypes": [
                {
                    "$id": "41",
                    "metalType": "14K Rose Gold"
                },
                {
                    "$id": "42",
                    "metalType": "14K White Gold"
                },
                {
                    "$id": "43",
                    "metalType": "14K Yellow Gold"
                },
                {
                    "$id": "44",
                    "metalType": "18K Rose Gold"
                },
                {
                    "$id": "45",
                    "metalType": "18K White Gold"
                },
                {
                    "$id": "46",
                    "metalType": "18K Yellow Gold"
                },
                {
                    "$id": "47",
                    "metalType": "Platinum"
                }
            ],
            "collections": [
                {
                    "$id": "48",
                    "collectionName": "Fancy Shape"
                }
            ],
            "allowableDiamondShapes": [
                {
                    "$id": "49",
                    "allowableDiamondShape": "Asscher"
                },
                {
                    "$id": "50",
                    "allowableDiamondShape": "Round"
                },
                {
                    "$id": "51",
                    "allowableDiamondShape": "Emerald"
                },
                {
                    "$id": "52",
                    "allowableDiamondShape": "Heart"
                },
                {
                    "$id": "53",
                    "allowableDiamondShape": "Marquise"
                },
                {
                    "$id": "54",
                    "allowableDiamondShape": "Oval"
                },
                {
                    "$id": "55",
                    "allowableDiamondShape": "Pear"
                },
                {
                    "$id": "56",
                    "allowableDiamondShape": "Princess"
                },
                {
                    "$id": "57",
                    "allowableDiamondShape": "Radiant"
                },
                {
                    "$id": "58",
                    "allowableDiamondShape": "Round"
                }
            ],
            "priceSettingId": 2694146,
            "videoURL": "https://www.overnightmountings.com/gemfind/library/Download_video_files_for_individual_styles/85134/85134.video.white.mp4",
            "showPrice": true
        },
        {
            "$id": "59",
            "settingId": 2693146,
            "vendorId": "2906",
            "name": "DOUBLE PRONG PS ENGAGEMENT RING",
            "imageUrl": "https://www.gemfind.net/jewelryimages/2906/media/productimages/84957.jpg",
            "cost": 616.0,
            "centerStoneMinCarat": "0.73",
            "centerStoneMaxCarat": "0.89",
            "originalCost": "",
            "mainImageURL": "https://www.gemfind.net/jewelryimages/2906/media/productimages/84957.jpg",
            "roundImageURL": "",
            "asscherImageURL": "",
            "emeraldImageURL": "",
            "radiantImageURL": "",
            "cushionImageURL": "",
            "marquiseImageURL": "",
            "ovalImageURL": "",
            "heartImageURL": "",
            "pearImageURL": "",
            "princessImageURL": "",
            "dealerId": "2803",
            "stockNumber": "84957",
            "isFavorite": false,
            "favId": null,
            "designerLogo": "https://www.gemfind.net/ResizeLogo.aspx?img=http://gemfind.net/dealerid_display/dealerimages/886660384logo_500x500_Wbg.png&wi=200&hi=200",
            "currencyFrom": "USD",
            "currencySymbol": "US$",
            "metalTypes": [
                {
                    "$id": "60",
                    "metalType": "14K Rose Gold"
                },
                {
                    "$id": "61",
                    "metalType": "14K White Gold"
                },
                {
                    "$id": "62",
                    "metalType": "14K Yellow Gold"
                },
                {
                    "$id": "63",
                    "metalType": "18K Rose Gold"
                },
                {
                    "$id": "64",
                    "metalType": "18K White Gold"
                },
                {
                    "$id": "65",
                    "metalType": "18K Yellow Gold"
                },
                {
                    "$id": "66",
                    "metalType": "Platinum"
                }
            ],
            "collections": [
                {
                    "$id": "67",
                    "collectionName": "Solitaire"
                }
            ],
            "allowableDiamondShapes": [
                {
                    "$id": "68",
                    "allowableDiamondShape": "Pear"
                }
            ],
            "priceSettingId": 2693861,
            "videoURL": "https://www.overnightmountings.com/gemfind/library/Download_video_files_for_individual_styles/84957/84957.video.white.mp4",
            "showPrice": true
        },
        {
            "$id": "69",
            "settingId": 2678923,
            "vendorId": "2906",
            "name": "ENGAGEMENT RINGS PS HALO",
            "imageUrl": "https://www.gemfind.net/jewelryimages/2906/media/productimages/50950-e.jpg",
            "cost": 1585.0,
            "centerStoneMinCarat": "0.73",
            "centerStoneMaxCarat": "0.89",
            "originalCost": "",
            "mainImageURL": "https://www.gemfind.net/jewelryimages/2906/media/productimages/50950-e.jpg",
            "roundImageURL": "",
            "asscherImageURL": "",
            "emeraldImageURL": "",
            "radiantImageURL": "",
            "cushionImageURL": "",
            "marquiseImageURL": "",
            "ovalImageURL": "",
            "heartImageURL": "",
            "pearImageURL": "",
            "princessImageURL": "",
            "dealerId": "2803",
            "stockNumber": "50950",
            "isFavorite": false,
            "favId": null,
            "designerLogo": "https://www.gemfind.net/ResizeLogo.aspx?img=http://gemfind.net/dealerid_display/dealerimages/886660384logo_500x500_Wbg.png&wi=200&hi=200",
            "currencyFrom": "USD",
            "currencySymbol": "US$",
            "metalTypes": [
                {
                    "$id": "70",
                    "metalType": "14K Rose Gold"
                },
                {
                    "$id": "71",
                    "metalType": "14K White Gold"
                },
                {
                    "$id": "72",
                    "metalType": "14K Yellow Gold"
                },
                {
                    "$id": "73",
                    "metalType": "18K Rose Gold"
                },
                {
                    "$id": "74",
                    "metalType": "18K White Gold"
                },
                {
                    "$id": "75",
                    "metalType": "18K Yellow Gold"
                },
                {
                    "$id": "76",
                    "metalType": "Platinum"
                }
            ],
            "collections": [
                {
                    "$id": "77",
                    "collectionName": "Halo"
                }
            ],
            "allowableDiamondShapes": [
                {
                    "$id": "78",
                    "allowableDiamondShape": "Pear"
                }
            ],
            "priceSettingId": 2682428,
            "videoURL": "https://www.overnightmountings.com/gemfind/library/Download_video_files_for_individual_styles/50950/50950.video.white.mp4",
            "showPrice": true
        },
        {
            "$id": "79",
            "settingId": 2689036,
            "vendorId": "2906",
            "name": "ENGAGEMENT RINGS HALO PEAR & TRILLION",
            "imageUrl": "https://www.gemfind.net/jewelryimages/2906/media/productimages/83498.jpg",
            "cost": 955.0,
            "centerStoneMinCarat": "0.73",
            "centerStoneMaxCarat": "0.89",
            "originalCost": "",
            "mainImageURL": "https://www.gemfind.net/jewelryimages/2906/media/productimages/83498.jpg",
            "roundImageURL": "",
            "asscherImageURL": "",
            "emeraldImageURL": "",
            "radiantImageURL": "",
            "cushionImageURL": "",
            "marquiseImageURL": "",
            "ovalImageURL": "",
            "heartImageURL": "",
            "pearImageURL": "",
            "princessImageURL": "",
            "dealerId": "2803",
            "stockNumber": "83498",
            "isFavorite": false,
            "favId": null,
            "designerLogo": "https://www.gemfind.net/ResizeLogo.aspx?img=http://gemfind.net/dealerid_display/dealerimages/886660384logo_500x500_Wbg.png&wi=200&hi=200",
            "currencyFrom": "USD",
            "currencySymbol": "US$",
            "metalTypes": [
                {
                    "$id": "80",
                    "metalType": "14K Rose Gold"
                },
                {
                    "$id": "81",
                    "metalType": "14K White Gold"
                },
                {
                    "$id": "82",
                    "metalType": "14K Yellow Gold"
                },
                {
                    "$id": "83",
                    "metalType": "18K Rose Gold"
                },
                {
                    "$id": "84",
                    "metalType": "18K White Gold"
                },
                {
                    "$id": "85",
                    "metalType": "18K Yellow Gold"
                },
                {
                    "$id": "86",
                    "metalType": "Platinum"
                }
            ],
            "collections": [
                {
                    "$id": "87",
                    "collectionName": "Halo"
                }
            ],
            "allowableDiamondShapes": [
                {
                    "$id": "88",
                    "allowableDiamondShape": "Pear"
                }
            ],
            "priceSettingId": 2689604,
            "videoURL": "https://www.overnightmountings.com/gemfind/library/Download_video_files_for_individual_styles/83498/83498.video.white.mp4",
            "showPrice": true
        }
    ],
    "count": 6
}

它在集合视图中显示所有产品。现在,当我单击特定产品时,即在集合视图的didselectitem方法中,我需要将该产品数据单独传递给下一个视图控制器。我想将该特定的数组传递给仅产品。 我该如何实现?

1 个答案:

答案 0 :(得分:0)

使用原始数据不是一个好主意。创建类似这样的模型

struct Model {
    let id: String
    let message: String
    let mountainList: Dictionary<String, Any> 
}

您还可以为mountainList创建另一个模型。然后,当您获取数据时,只需将其插入模型中即可。这样会容易得多。当您点击特定的索引路径时,只需在该索引处传递模型即可。您也可以使用可编码。 看看这个answer