2017-07-14 47 views
0

我花了很多時間編寫無意義的代碼來生成模型類。 例如:這裏是JSON數據如何通過json(Dictionary)生成模型類或向現有模型類添加屬性(無屬性)

{ 
    "id": 1, 
    "productcode": "5012014032", 
    "productname": "通用點數", 
    "categoryid": null, 
    "supplierid": null, 
    "inprice": null, 
    "unit": "¥", 
    "barcode": "2546138769542", 
    "outprice": 1500, 
    "safestock": null, 
    "bdate": 1483200000000, 
    "type": null, 
    "detail": "這是通用點數", 
    "ext": null, 
    "ext2": null, 
    "points": "15", 
    "validmonth": 3, 
    "photoid": 2, 
    "path": "/upload/26-9449705-1477983341-.jpg", 
    "productid": 1 
} 

然後,我創建類產品:

class Product: NSObject { 
let id: String 
let productcode: String 
let productname: String 
let categoryid: String 
let supplierid: String 
let inprice: String 
let unit: String 
let barcode: String 
let outprice: String 
let safestock: String 
let bdate: String 
let type: String 
let detail: String 
let ext: String 
let ext2: String 
let points: String 
let validmonth: String 
let photoid: String 
let path: String 
let productid: String 

init(id: String, productcode: String, productname: String, categoryid: String, supplierid: String, inprice: String, unit: String, barcode: String, outprice: String, safestock: String, bdate: String, type: String, detail: String, ext: String, ext2: String, points: String, validmonth: String, photoid: String, path: String, productid: String) { 
    self.id = id 
    self.productcode = productcode 
    self.productname = productname 
    self.categoryid = categoryid 
    self.supplierid = supplierid 
    self.inprice = inprice 
    self.unit = unit 
    self.barcode = barcode 
    self.outprice = outprice 
    self.safestock = safestock 
    self.bdate = bdate 
    self.type = type 
    self.detail = detail 
    self.ext = ext 
    self.ext2 = ext2 
    self.points = points 
    self.validmonth = validmonth 
    self.photoid = photoid 
    self.path = path 
    self.productid = productid 
}} 

這太boring.Now我想知道是否有可能通過JSON編程方式生成相應的類或添加屬性上課沒有財產。

希望你知道我說的是什麼。SWIFT和Objective-C都是可以接受的。

感謝您的任何建議。

回答

0

請參考下面的鏈接,並在這個應用程序中傳遞你的JSON數據。

https://github.com/Ahmed-Ali/JSONExport

1)克隆的項目。

2)在Xcode中運行項目。

3)將您的JOSN響應粘貼到您運行的應用程序中。

4)現在按保存按鈕。

5)你有模型swift文件。

相關問題