如何在领域swift中实现json
我有一本json格式的字典。我想在大约1000个单元格中用一个表来显示它,并将它保存到领域数据库中。我是数据库新手,谁能告诉我如何实现这个?我应该尝试在应用程序外部转换格式,还是在使用json时加载json 我的字典看起来像这样如何在领域swift中实现json,swift,realm,Swift,Realm,我有一本json格式的字典。我想在大约1000个单元格中用一个表来显示它,并将它保存到领域数据库中。我是数据库新手,谁能告诉我如何实现这个?我应该尝试在应用程序外部转换格式,还是在使用json时加载json 我的字典看起来像这样 [ { "id":0, "book":1, "lesson":1, "kanji":"\u4e2d\u56fd\u4eba" } { "id":1, "book":1,
[
{
"id":0,
"book":1,
"lesson":1,
"kanji":"\u4e2d\u56fd\u4eba"
} {
"id":1,
"book":1,
"lesson":1,
"kanji":"\u65e5\u672c\u4eba"
},
...
]
首先,您需要安装2个吊舱:
pod 'SwiftyJSON' #great for handling JSON
pod 'RealmSwift' #Realm database
您需要创建能够保存在领域中的对象。我想你的目标是某种类型的当然或类似的东西。您可以根据需要重命名它:
import Foundation
import RealmSwift
import SwiftyJSON
class Course: Object {
@objc dynamic var id = 0
@objc dynamic var book = 0
@objc dynamic var lesson = 0
@objc dynamic var kanji = ""
override static func primaryKey() -> String? { //you need this in case you will want to update this object in Realm
return "id"
}
convenience init(courseJson: JSON) {
self.id = courseJson["id"].intValue
self.book = courseJson["book"].intValue
self.lesson = courseJson["lesson"].intValue
self.kanji = courseJson["kanji"].stringValue
}
}
现在,在从服务器获取json的地方,您需要执行以下操作:
var courses = [Course]()
let jsonArray = JSON(dataFromServer).arrayValue
for courseJson in jsonArray {
let course = Course(courseJson: courseJson)
courses.append(course)
}
//Now, you need to save these objects in Realm:
let realm = try! Realm()
try! realm.write {
realm.add(courses, update: true)
}
在要显示数据的视图控制器的viewDidLoad方法中,您需要从领域中获取所有课程对象,将它们添加到数组中,并在tableView/collectionView中显示数据:
import UIKit
import RealmSwift
class PresentingCoursesVC: UIViewController {
//MARK: IBOutlets
@IBOutlet weak var tableView: UITableView!
//MARK: Properties
var courses = Results<Course>?
//MARK: Lifecycles
override func viewDidLoad() {
super.viewDidLoad()
getCourses()
}
//MARK: Private functions
private func getCourses() {
let realm = try! Realm()
courses = realm.objects(Course.self).sorted(byKeyPath: "id", ascending: true)
tableView.reloadData()
}
}
//MARK: TableView Datasource
extension PresentingCoursesVC: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return courses?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCustomCell") as! MyCustomCell
let course = courses[indexPath.row]
cell.setupCell(course: course)
return cell
}
}
我希望我的回答是有帮助的 请发布一些字典的结构。解析JSON并提取密钥以相应地创建领域模型。另外,领域文档很容易理解,我建议您深入了解它。它看起来就像是一个基本字典,每个子字典都有键:值对。因此,创建一个具有4个属性的领域对象,通过init将子字典id、book、lesson、kanji传递给领域对象,或者传递一个函数来填充其属性并将其保存到领域。还有别的吗?
var courses = [Course]()
let jsonArray = JSON(dataFromServer).arrayValue
for courseJson in jsonArray {
let course = Course(courseJson: courseJson)
courses.append(course)
}
//Now, you need to save these objects in Realm:
let realm = try! Realm()
try! realm.write {
realm.add(courses, update: true)
}
import UIKit
import RealmSwift
class PresentingCoursesVC: UIViewController {
//MARK: IBOutlets
@IBOutlet weak var tableView: UITableView!
//MARK: Properties
var courses = Results<Course>?
//MARK: Lifecycles
override func viewDidLoad() {
super.viewDidLoad()
getCourses()
}
//MARK: Private functions
private func getCourses() {
let realm = try! Realm()
courses = realm.objects(Course.self).sorted(byKeyPath: "id", ascending: true)
tableView.reloadData()
}
}
//MARK: TableView Datasource
extension PresentingCoursesVC: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return courses?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCustomCell") as! MyCustomCell
let course = courses[indexPath.row]
cell.setupCell(course: course)
return cell
}
}