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

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Wie erstelle ich in Swift eine Modellklasse für ein Wörterbuch innerhalb eines Arrays?

by Guest » 19 Jan 2025, 21:09

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?

Top