iOS创建对象最佳实践 [英] iOS Creating an Object Best Practice

查看:123
本文介绍了iOS创建对象最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个向导,供用户使用我的应用进行注册,但是,我对如何在途中存储他们的信息存在疑问。



我有一个用户模型,在用户从数据库中提取时会填写,但是,这里有一些必需的字段如果我将它用作用户通过向导时传递的对象,则不会填写的模型。



这是我的用户型号:

 最终类用户: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?(响应:NSHTTPURLResponse,var表示:AnyObject){
如果让dataRepresentation =((表示为!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(名称:ThomasBaldwin)

如果让timeCreated = representation.valueForKeyPath(time_created)为?字符串{
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(日期:NSDate())
}
}

静态func集合(响应响应:NSHTTPURLResponse,表示:AnyObject) - > ; [用户] {
var users:[User] = []

如果让dataRepresentation =((表示为!NSDictionary).valueForKey(data)为?[NSDictionary]){如果让dataRepresentation = dataRepresentation为
? [[String:AnyObject]] {
for dataRepresentation in dataRepresentation {
if let user = User(response:response,representation:userRepresentation){
users.append(user)
}
}
}
}

返回用户
}
}

注意变量 id timeCreated 。这些都是在将新行添加到数据库中的 Users 表时生成的,因此,在实际创建用户之前,我没有这些变量的值。 / p>

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



我的问题是,我应该


A。只需使这些常量可选,并将这些方法添加到我当前的用户模型

B。制作另一个名为 CreateUserModel 的模型,它只包含变量用户将填写的信息并将额外的方法放在那里



更新



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



这是我更新的用户类:

  final class用户:NSObject,ResponseObjectSerializable,ResponseCollectionSerializable {

var properties = NSDictionary()

init?(响应:NSHTTPURLResponse ,representation:AnyObject){
如果让dataRepresentation =((表示为!NSDictionary).valueForKey(data)为?[String:AnyObject]){
properties = dataRepresentation
}

properties =表示为! NSDictionary
}

static func collection(响应响应:NSHTTPURLResponse,表示:AnyObject) - > [用户] {
var users:[User] = []

如果让dataRepresentation =((表示为!NSDictionary).valueForKey(data)为?[NSDictionary]){如果让dataRepresentation = dataRepresentation为
? [[String:AnyObject]] {
for dataRepresentation in dataRepresentation {
if let user = User(response:response,representation:userRepresentation){
users.append(user)
}
}
}
}

返回用户
}
}


解决方案

我会让他们成为Optionals。这是Optionals的美妙之处 - 您可以使用 nil 来表示此处没有数据。



其他想到的大策略是使用字典作为模型中的存储机制,因为这样既可以有某个键,也可以不使用。您可以通过将键传递到字典来使您的User对象键值编码兼容,从而有效透明。


I've created a wizard for user's to sign up using my app, however, I'm having some doubts as to how I should store their information along the way.

I have a User model, which is filled out when users are pulled from the database, however, there are some required fields on this model that wouldn't be filled out if I were to use it as the object that is passed along as the user goes through the the wizard.

Here is my User model:

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

Notice the variables id and timeCreated. These are both generated when a new row is added to the Users table in the database, therefore, I wouldn't have values for those variables until the user is actually created.

Also, I would like to add some methods to the model, such as validateUser which will be a method that makes sure all the fields are filled out, and validateEmail which will be a method that makes sure the email is in proper syntax, and so on...

My question is, should I

A. just make those constants optional and add those methods to my current User model
B. make another model called CreateUserModel that only has variables for the information the user will be filling out and put the extra methods in there

UPDATE

I updated my User class to use a dictionary as the storage mechanism and it already looks a lot cleaner. However, the issue that comes to mind is, how will another programmer know which fields he can grab from the User model since I'm not individually storing them as variables anymore. Would they just have to check the DB and look at the structure of the table?

Here's my updated User 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
    }
}

解决方案

I would make them Optionals. That is the beauty of Optionals - you can use nil to mean exactly "no data here".

The other grand strategy that comes to mind is to use a dictionary as the storage mechanism inside your model, because that way either it has a certain key or it doesn't. You could make your User object key-value coding compliant, and thus effectively transparent, by passing keys on to the dictionary.

这篇关于iOS创建对象最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆