iOS 创建对象最佳实践

iOS Creating an Object Best Practice

我已经为用户创建了一个向导来使用我的应用程序进行注册,但是,我对我应该如何在整个过程中存储他们的信息有一些疑问。

我有一个 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
    }
}

注意变量 idtimeCreated。这些都是在将新行添加到数据库中的 Users table 时生成的,因此,在实际创建用户之前我不会有这些变量的值。

此外,我想向模型添加一些方法,例如 validateUser 将是一种确保所有字段都已填写的方法,而 validateEmail 将是一种确保电子邮件语法正确的方法,等等...

我的问题是,我应该

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

更新

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

这是我更新的用户 class:

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
    }
}

我会让它们成为可选项。这就是 Optionals 的美妙之处——你可以使用 nil 来表示 "no data here".

想到的另一个重要策略是使用字典作为模型内部的存储机制,因为那样的话它要么有某个键,要么没有。通过将键传递给字典,您可以使您的用户对象键值编码兼容,从而有效透明。