[英]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
}
}
注意变量id
和timeCreated
。 它们都是在数据库中向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
}
}
我将它们设为可选。 这就是Optionals的优点-您可以使用nil
来表示“此处没有数据”。
我想到的另一种宏伟的策略是使用字典作为模型内部的存储机制,因为这样既可以有一个特定的键,也可以没有。 通过将键传递给字典,可以使您的User对象键值编码兼容,从而有效地透明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.