【问题标题】:Swift Best Practice For Models模型的 Swift 最佳实践
【发布时间】:2015-10-25 05:07:48
【问题描述】:

我创建了一个User 类,并获得了使用字典作为存储机制的建议,以使其更灵活并满足我的需求。请参考此question

这是我原来的User 模型:

final class User: NSObject, ResponseObjectSerializable, ResponseCollectionSerializable {
    let id: Int
    var facebookUID: String?
    var email: String
    var firstName: String
    var lastName: String
    var phone: String?
    var position: String?
    var thumbnail: UIImage?
    var timeCreated: CVDate

    init?(response: NSHTTPURLResponse, var representation: AnyObject) {
        if let dataRepresentation = ((representation as! NSDictionary).valueForKey("data") as? [String: AnyObject]) {
            representation = dataRepresentation
        }

        self.id = representation.valueForKeyPath("id") as! Int
        self.facebookUID = (representation.valueForKeyPath("facebook_UID") as? String)
        self.email = (representation.valueForKeyPath("email") as? String) ?? ""
        self.firstName = (representation.valueForKeyPath("first_name") as? String) ?? ""
        self.lastName = (representation.valueForKeyPath("last_name") as? String) ?? ""
        self.phone = (representation.valueForKeyPath("phone") as? String)
        self.position = (representation.valueForKeyPath("position_name") as? String)
        self.thumbnail = UIImage(named: "ThomasBaldwin")

        if let timeCreated = representation.valueForKeyPath("time_created") as? String {
            let formatter = NSDateFormatter()
            formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
            if let date = formatter.dateFromString(timeCreated) {
                self.timeCreated = CVDate(date: date)
            } else {
                self.timeCreated = CVDate(date: NSDate())
            }
        } else {
            self.timeCreated = CVDate(date: NSDate())
        }
    }

    static func collection(response response: NSHTTPURLResponse, representation: AnyObject) -> [User] {
        var users: [User] = []

        if let dataRepresentation = ((representation as! NSDictionary).valueForKey("data") as? [NSDictionary]) {
            if let dataRepresentation = dataRepresentation as? [[String: AnyObject]] {
                for userRepresentation in dataRepresentation {
                    if let user = User(response: response, representation: userRepresentation) {
                        users.append(user)
                    }
                }
            }
        }

        return users
    }
}

这是我更新的User 类,使用字典作为存储机制:

final class User: NSObject, ResponseObjectSerializable, ResponseCollectionSerializable {

    var properties = NSDictionary()

    init?(response: NSHTTPURLResponse, representation: AnyObject) {
        if let dataRepresentation = ((representation as! NSDictionary).valueForKey("data") as? [String: AnyObject]) {
            properties = dataRepresentation
        }

        properties = representation as! NSDictionary
    }

    static func collection(response response: NSHTTPURLResponse, representation: AnyObject) -> [User] {
        var users: [User] = []

        if let dataRepresentation = ((representation as! NSDictionary).valueForKey("data") as? [NSDictionary]) {
            if let dataRepresentation = dataRepresentation as? [[String: AnyObject]] {
                for userRepresentation in dataRepresentation {
                    if let user = User(response: response, representation: userRepresentation) {
                        users.append(user)
                    }
                }
            }
        }

        return users
    }
}

但我想到的问题是,另一个程序员如何知道他可以从User 模型中获取哪些字段,因为我不再将它们单独存储为变量。他们是否只需要检查数据库并查看表的结构?

现在User 类的设置方式也是如此,它几乎可以是我从数据库中提取的任何表的模型(因为我现在没有User 模型特定的方法),我可以总是为任何桌子打电话user.properties.valueForKeyPath("column_name")

如果我将模型名称更改为更广泛的名称,并将其重用于要从中提取数据且不需要任何模型特定方法的任何表,这是否是一种干净的做法?

【问题讨论】:

  • 我同意@duncanC,但如果你确实想保留一个内部字典存储,你应该为各种属性创建离散的计算属性并使用代码来访问底层字典

标签: ios swift swift2


【解决方案1】:

比起字典,我更喜欢自定义数据对象。自定义数据对象具有具体类型的特定命名字段。它是自我记录的,当您使用它时,您可以假设它具有您期望的属性并且它们是正确的类型。使用字典你也不能假设(好吧,使用 Swift 字典你可以强制输入,但是你不能混合不同类型的属性,比如名称字符串和数字工资值)

【讨论】:

    猜你喜欢
    • 2015-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多