Wie erstelle ich in Swift eine Modellklasse für ein Wörterbuch innerhalb eines Arrays?IOS

Programmierung für iOS
Guest
 Wie erstelle ich in Swift eine Modellklasse für ein Wörterbuch innerhalb eines Arrays?

Post by Guest »

Hier ist mein JSON:

Code: Select all

[ {
"id": 6854,
"name": "Laundry Iron",
"images": [
{
"id": 6856,
"src": "https://abcd.com/yzx/uploads/1750.jpg",
}
],

} ]
Wie erstellen wir eine Modellklasse zum Abrufen von „images“:[“src“: „String“ ]? Ich möchte „src“ abrufen. Ich habe es versucht, aber es funktioniert nicht:

Code: Select all

class ProductModel {

var title: String?
var regularPrice: Int?
var salePrice: Int?
var productDescroption: String?
var imageUrl: [ImageUrl]?

init(productJsonL: NSDictionary) {
self.title = productJsonL["name"] as? String
self.regularPrice = productJsonL["regular_price"] as? Int
self.salePrice = productJsonL["sale_price"] as? Int
self.productDescroption = productJsonL["description"] as? String
//The problem is here ........
//self.imageUrl = productJsonL["images"]![0]!["src"] as? String
self.imageUrl = ImageUrl(imageUrlJson: (productJsonL["images"]![0] as? NSDictionary)!)

}
}

class ImageUrl {

var source: String?
init(imageUrlJson: NSDictionary) {
self.source = imageUrlJson["src"] as? String
}

}
Bitte korrigieren Sie mich mit der Struktur, die ich oben gemacht habe, damit ich alles auf einmal in einem Array anhängen kann?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post