Return 更具体 objects 来自 Swift 中的数据模型
Return more concrete objects from Data Model in Swift
我正在开发一个从 Firebase 获取数据的应用程序。我将向您展示的代码工作得很好,但即使作为菜鸟,我 也知道 实施应该更好。我真的不知道如何重构它。
这里是 TLDR:
我创建了一个用于发布博客帖子的数据模型。我为此使用了一个结构,在初始版本中,我所有的属性都是字符串类型。
但是,除了标题和摘要的字符串外,我的帖子还包含图像的 URL 和日期(post 日期)。
我希望能够从我的 BlogPost return object 更具体的对象,例如已经创建的 URL 或日期 object。
就像我说的,我知道我的实现很糟糕,我想学习一种更好的类型转换方法,以便我可以实现上述行为。
这是我的 BlogPost 数据模型的实现:
import Foundation
import FirebaseDatabase
struct BlogPost {
let key: String
let title: String
let body: String
let summary: String
let author: String?
let liveSince: Date
let featuredImageLink: URL?
let itemReference:DatabaseReference?
init(snapshot: DataSnapshot) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-mm-dd"
key = snapshot.key
itemReference = snapshot.ref
if let snapshotDictionary = snapshot.value as? NSDictionary, let postTitle = snapshotDictionary["Title"] as? String {
title = postTitle
} else {
title = "Cannot display Title for this item :("
}
if let snapshotDictionary = snapshot.value as? NSDictionary, let postBody = snapshotDictionary["Body"] as? String {
body = postBody
} else {
body = "Cannot display Body for this item :("
}
if let snapshotDictionary = snapshot.value as? NSDictionary, let postSummary = snapshotDictionary["Summary"] as? String {
summary = postSummary
} else {
summary = "Due to some weird error, the Summary for this item cannot be displayed. Insert more coffee and Pizza in developer"
}
if let snapshotDictionary = snapshot.value as? NSDictionary, let postAuthor = snapshotDictionary["Author"] as? String {
author = postAuthor
} else {
author = "Nobody wrote this :("
}
if let snapshotDictionary = snapshot.value as? NSDictionary, let postImageLink = snapshotDictionary["FeaturedImage"] as? String {
featuredImageLink = URL(string: postImageLink)!
} else {
featuredImageLink = URL(string:"https://someimagelink")!
}
if let snapshotDictionary = snapshot.value as? NSDictionary, let liveDate = snapshotDictionary["LiveSince"] as? String {
if let live = dateFormatter.date(from: liveDate) {
liveSince = live
} else {
liveSince = dateFormatter.date(from: "1990-06-26")!
}
} else {
liveSince = dateFormatter.date(from: "1990-06-26")!
}
}
}
非常欢迎任何建设性的反馈,因为我真的很想了解如何正确地做到这一点,或者一开始这样做是否有意义!
非常感谢您的提前回复!
我有一些建议。看起来您在将 snapshsot.value 转换为 NSDictionary 时继续使用条件绑定来展开它。既然解包这个值是解包其他值的先决条件,为什么不使用 guard 语句来解包呢?在你的 guard 语句中,你可以默认初始化结构中的所有属性。或者,如果你不坚持默认初始化你的属性,你可以只使用一个可失败的初始化器,并在 guard 语句中只使用 return nil。
guard let snapshotDictionary = snapshot.value as? NSDictionary else {
title = "Cannot display Title for this item :("
body = "Cannot display Body for this item :("
summary = "Due to some weird error, the Summary for this item cannot be
displayed. Insert more coffee and Pizza in developer"
...
...
}
我正在开发一个从 Firebase 获取数据的应用程序。我将向您展示的代码工作得很好,但即使作为菜鸟,我 也知道 实施应该更好。我真的不知道如何重构它。
这里是 TLDR:
我创建了一个用于发布博客帖子的数据模型。我为此使用了一个结构,在初始版本中,我所有的属性都是字符串类型。
但是,除了标题和摘要的字符串外,我的帖子还包含图像的 URL 和日期(post 日期)。
我希望能够从我的 BlogPost return object 更具体的对象,例如已经创建的 URL 或日期 object。
就像我说的,我知道我的实现很糟糕,我想学习一种更好的类型转换方法,以便我可以实现上述行为。
这是我的 BlogPost 数据模型的实现:
import Foundation
import FirebaseDatabase
struct BlogPost {
let key: String
let title: String
let body: String
let summary: String
let author: String?
let liveSince: Date
let featuredImageLink: URL?
let itemReference:DatabaseReference?
init(snapshot: DataSnapshot) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-mm-dd"
key = snapshot.key
itemReference = snapshot.ref
if let snapshotDictionary = snapshot.value as? NSDictionary, let postTitle = snapshotDictionary["Title"] as? String {
title = postTitle
} else {
title = "Cannot display Title for this item :("
}
if let snapshotDictionary = snapshot.value as? NSDictionary, let postBody = snapshotDictionary["Body"] as? String {
body = postBody
} else {
body = "Cannot display Body for this item :("
}
if let snapshotDictionary = snapshot.value as? NSDictionary, let postSummary = snapshotDictionary["Summary"] as? String {
summary = postSummary
} else {
summary = "Due to some weird error, the Summary for this item cannot be displayed. Insert more coffee and Pizza in developer"
}
if let snapshotDictionary = snapshot.value as? NSDictionary, let postAuthor = snapshotDictionary["Author"] as? String {
author = postAuthor
} else {
author = "Nobody wrote this :("
}
if let snapshotDictionary = snapshot.value as? NSDictionary, let postImageLink = snapshotDictionary["FeaturedImage"] as? String {
featuredImageLink = URL(string: postImageLink)!
} else {
featuredImageLink = URL(string:"https://someimagelink")!
}
if let snapshotDictionary = snapshot.value as? NSDictionary, let liveDate = snapshotDictionary["LiveSince"] as? String {
if let live = dateFormatter.date(from: liveDate) {
liveSince = live
} else {
liveSince = dateFormatter.date(from: "1990-06-26")!
}
} else {
liveSince = dateFormatter.date(from: "1990-06-26")!
}
}
}
非常欢迎任何建设性的反馈,因为我真的很想了解如何正确地做到这一点,或者一开始这样做是否有意义!
非常感谢您的提前回复!
我有一些建议。看起来您在将 snapshsot.value 转换为 NSDictionary 时继续使用条件绑定来展开它。既然解包这个值是解包其他值的先决条件,为什么不使用 guard 语句来解包呢?在你的 guard 语句中,你可以默认初始化结构中的所有属性。或者,如果你不坚持默认初始化你的属性,你可以只使用一个可失败的初始化器,并在 guard 语句中只使用 return nil。
guard let snapshotDictionary = snapshot.value as? NSDictionary else {
title = "Cannot display Title for this item :("
body = "Cannot display Body for this item :("
summary = "Due to some weird error, the Summary for this item cannot be
displayed. Insert more coffee and Pizza in developer"
...
...
}