斯威夫特模型的最佳实践

Tho*_*mas 1 ios swift swift2

我创建了一个User类,并给出了一个建议,使用字典作为存储机制,使其更灵活,满足我的需求.请参考这个问题.

这是我的原始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
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我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
    }
}
Run Code Online (Sandbox Code Playgroud)

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

User现在这个类的设置方式,它几乎可以是我从数据库中提取的任何表的模型(因为我现在没有User模型特定的方法),我总是可以调用user.properties.valueForKeyPath("column_name")任何表.

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

Dun*_*n C 5

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