大家下午好,
我目前正致力于一项名为" Bookmark"的任务。简短描述是每当我点击书签按钮时,应用程序将保存文章,然后将其显示在BookmarkVC中。
我有3种类型的对象称为"新闻","文件"和#34; ITSectionResult"我的想法是创建一个名为" BookmarkItem"其中包含3个上述对象的元素和一个名为bookmarkCategory的属性,用于指示对象的类型,以便我可以使用它在BookmarkVC中显示。我只想在一个领域对象上工作,所以任何人都可以帮我一个想法将这些人聚集在一起吗?在这里,我将附上我的草案代码如下:
对于新闻类:
class NewsArticle:Object {
dynamic var title: String?
dynamic var body:String?
dynamic var category:String?
dynamic var createTime:String?
dynamic var image:String?
dynamic var id:String?
convenience init (title:String, body:String, category:String,
image:String, id:String, createTime:String) {
self.init()
self.title = title
self.body = body
self.category = category
self.image = image
self.id = id
self.createTime = createTime
}
文档类:
class Documents {
var id: String?
var title:String?
var createTime:CLong?
var url:String?
init (title:String, id:String, createTime:CLong?, url:String) {
self.title = title
self.id = id
self.createTime = createTime
self.url = url
}
ITSectionResult类:
class SectionSearchResult {
var title:String?
var image:String?
var id:String?
var byCategory:String?
init (title:String, image:String, id:String, byCategory:String) {
self.title = title
self.image = image
self.id = id
self.byCategory = byCategory
}
最后起草了BookmarkItem类:
class BookmarkItem:Object {
//Category
dynamic var bookmarkCategory:BookMarkItemCategory?
dynamic var title: NewsArticle?
dynamic var body:NewsArticle?
dynamic var category:NewsArticle?
dynamic var createTime:NewsArticle?
dynamic var image:NewsArticle?
dynamic var id:NewsArticle?
dynamic var link:String?
dynamic var url:String?
}
class BookMarkItemCategory:Object {
dynamic var name = ""
}
这里我要提醒一下,BookmarkItem类使用3个主要属性在BookmarkVC中显示," image"对于过滤器类别类型(例如:Documents对象的book图像,News对象的报纸图标)," title"用于在WebView中显示的标题和URL。非常感谢你,祝你们周末愉快。
答案 0 :(得分:1)
正如EpicPandaForce的评论中所提到的,这可以通过不为每种类型使用类来实现,而是使用统一模型。
import RealmSwift
final class UnifiedModel: Object {
dynamic var title: String = ""
dynamic var id: String = ""
dynamic var createTime: String = ""
dynamic var category: String = ""
dynamic var body: String?
dynamic var image: String?
override static func primaryKey() -> String {
return "id"
}
}
使用上面显示的模型,每个实例都需要一个标题,id,创建时间和类别,而body和image可以保持为零。如果您有任何其他问题,请随时跟进。
免责声明:我为Realm工作。