2017-07-14 109 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文件。

相关问题