我努力讓我的CoreData對象到JSON,以便我可以使用它發送到Web服務器。在Swift 3中JSON CoreData對象
這是如何我目前從CoreData取我的對象:
func fetchRecord() -> [Record] {
do {
records = try context.fetch(Record.fetchRequest())
} catch {
print("Error fetching data from CoreData")
}
return records
}
我能夠上顯示這對我的tableView這樣:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "recordCell", for: indexPath) as! RecordCell
cell.nameLbl.text = records[indexPath.row].name
cell.quantityLbl.text = "Quantity: \(String(records[indexPath.row].quantity))"
cell.dateLbl.text = dateString(date: records[indexPath.row].date)
return cell
}
我試圖環路我的要求內像這樣:
for rec in records {
print(rec)
}
給出了這個:
我已經閱讀了很多關於如何實現這個目標的方法,但是沒有一個看起來真的對我有益。這裏的大多數示例都顯示瞭如何將JSON轉換爲CoreData,而不是其他方式。有誰知道任何可以幫助我實現這個目標的好教程或文檔?
對不起,請查看更新的標題。有什麼辦法可以將它轉換成Swift嗎? – Chace
您能否解釋我會在哪裏使用此代碼? – Chace
請檢查swift版本,根據您的規範要發送此記錄對象到webservice,爲此,您要將recort對象轉換爲json。因此,只需將此添加到要將coredata對象inti json轉換的位置即可。 – KavyaKavita