我花了很多时间编写无意义的代码来生成模型类。例如:这里是json数据
{
"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
}}
太无聊了。现在我想知道是否可以通过json以编程方式生成相应的类或将属性添加到没有属性的类中。
希望你知道我说了什么。Swift 和 Objective-C 都是可以接受的。
感谢您的任何建议。