Ios 在Swift中从数据模型返回更多具体对象

Ios 在Swift中从数据模型返回更多具体对象,ios,json,struct,swift3,datamodel,Ios,Json,Struct,Swift3,Datamodel,我正在开发一个从Firebase获取数据的应用程序。我将向您展示的代码工作得很好,但即使是新手,我也知道实现应该更好。我真的不知道如何重构它 以下是TLDR: 我已经创建了一个数据模型,用于浏览博客文章。我为此使用了一个结构,在初始版本中,我的所有属性都是String类型 但是,除了标题和摘要字符串之外,我的帖子还包含一个指向图像的URL和一个日期(post Date) 我希望能够从BlogPost对象返回更具体的对象集,例如已经创建的URL或日期对象 正如我所说,我知道我的实现很糟糕,我想学习

我正在开发一个从Firebase获取数据的应用程序。我将向您展示的代码工作得很好,但即使是新手,我也知道实现应该更好。我真的不知道如何重构它

以下是TLDR:

我已经创建了一个数据模型,用于浏览博客文章。我为此使用了一个结构,在初始版本中,我的所有属性都是String类型

但是,除了标题和摘要字符串之外,我的帖子还包含一个指向图像的URL和一个日期(post Date)

我希望能够从BlogPost对象返回更具体的对象集,例如已经创建的URL或日期对象

正如我所说,我知道我的实现很糟糕,我想学习一种更好的类型转换方法,这样我就可以实现上述行为

以下是我的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语句中返回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"
    ...
    ...
}

那正是我最后要做的!谢谢你的回复!