iOS创建对象最佳实践

Tho*_*mas 0 ios swift ios8 ios9 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)

注意变量idtimeCreated.这些都是Users在向数据库中的表添加新行时生成的,因此,在实际创建用户之前,我没有这些变量的值.

此外,我想在模型中添加一些方法,例如,validateUser这将是一个确保填写所有字段validateEmail的方法,并且这将是确保电子邮件具有正确语法的方法,依此类推. ..

我的问题是,我应该

A.只是让那些常量可选,并将这些方法添加到我当前的User模型
B.创建另一个模型,调用CreateUserModel只有用户将填写的信息的变量,并将额外的方法放在那里

UPDATE

我更新了我的User类,使用字典作为存储机制,它已经看起来更清洁了.然而,想到的问题是,另一个程序员将​​如何知道他可以从User模型中获取哪些字段,因为我不再单独将它们存储为变量.他们只需要检查数据库并查看表的结构吗?

这是我更新的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)

mat*_*att 5

我会让他们成为Optionals.这是Optionals的美妙之处 - 你可以nil用来表示"这里没有数据".

想到的另一个宏大的策略是使用字典作为模型中的存储机制,因为它具有某个键或者没有.您可以通过将键传递到字典来使您的User对象键值编码符合,从而有效透明.