【发布时间】:2018-10-10 08:29:35
【问题描述】:
我在 swift 应用程序中通过 api 调用从服务器获取 JSON 数据。 因此,我想将其存储到 Realm 数据库中,并且需要再次获取以显示在 tableview 中。
我对Realm数据库一无所知,看了几个论坛后,我有了创建Object类的基本思路。
所以,我已经安装了 Realm pod 文件并将该库导入到我的类中。
我的 JSON 数据是
[{
"type": "story",
"story":
{
"author-name": "",
"headline": "Quotes ",
"summary": "Best quotes of Muhammad Ali",
"hero-image": "https://image”
}
},
{
"type": “Trending”,
"story":
{
"author-name": "",
"headline": "Quotes ",
"summary": "Best quotes of Muhammad Ali",
"hero-image": "https://image”
}
},
{
"type": “Technology”,
"story":
{
"author-name": "",
"headline": "Quotes ",
"summary": "Best quotes of Muhammad Ali",
"hero-image": "https://image”
}
},
{
"type": “Top”,
"story":
{
"author-name": "",
"headline": "Quotes ",
"summary": "Best quotes of Muhammad Ali",
"hero-image": "https://image”
}
}
]
而且我每个 type 关键字都有不同的模型类保存的数据从 api 数据显示在 Tableview
喜欢
let storyObj = StoryModule()
let trending = StoryModule()
let technology = StoryModule()
let stotopryObj1 = StoryModule()
我正在为每个 type
保存每个键值if abc.type == "story" {
let storyObj = abc.story
storyObj.authorname = storyObj?.authorname
storyObj.heroimage = storyObj?.heroimage
storyObj.headline = storyObj?.headline
storyObj.summary = storyObj?.summary
self.treningStoriesList.append(storyObj)
}
其余的 Trending、Top 和 Technology 对象也是如此。
Realm 模块是
import RealmSwift
class DemoInfo: Object {
@objc dynamic var category = ""
let items = List<DemoList>()
}
class DemoList : Object {
@objc dynamic var authorName = ""
@objc dynamic var imageUrl = ""
@objc dynamic var summary = ""
@objc dynamic var headLine = ""
}
在MainViewController 类中,
let realmDB = try! Realm()
但是,我被打动了,如何保存那些storyObj,technology,top, etc 模块数据并获取。
谁能推荐我?
【问题讨论】:
标签: ios iphone swift database realm