iOS 创建对象最佳实践

标签 ios swift ios8 swift2 ios9

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

我有一个 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 表时生成的,因此,在实际创建用户之前我不会有这些变量的值。

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

我的问题是,我应该

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

更新

我更新了我的 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
    }
}

最佳答案

我会让它们成为可选项。这就是 Optionals 的美妙之处——您可以使用 nil 来表示“这里没有数据”。

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

关于iOS 创建对象最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33249740/

相关文章:

swift - 下铸 Nib uitableviewcell

ios - 对 iOS 上的 asp.net Web 应用程序进行故障排除

ios - iOS 中这种情况的最佳设计解决方案是什么?

ios - 如何自动更新应用程序 iOS 应用程序

ios - 从水平 UIScrollView 中删除额外的底部填充

storyboard - 显示详细 View Controller 时iOS8 UIToolbar消失

iphone - 如何使用供应配置文件ios获取IPA文件

ios - didSelectRowAtIndexPath 时关闭 View 并设置数据

ios - 使用编码协议(protocol)解析 JSON nil 值

ios - 如何从模态 XIB 设置 VC 的背景颜色