错误处理解析 JSON 中Swift

发布时间: 2017/3/5 13:06:48
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我使用的 Alamofire 和我解析返回的 JSON 到的对象,如下所示︰

final class User: NSObject, ResponseObjectSerializable {
    var id: Int
    var facebookUID: String?
    var email: String
    var firstName: String
    var lastName: String
    var phone: String?
    var position: String?
    var timeCreated: CVDate

    init?(response: NSHTTPURLResponse, var representation: AnyObject) {
        if let dataRepresentation = ((representation as! NSDictionary).valueForKey("data") as? [String: AnyObject]) {
            representation = dataRepresentation
        }

        if let id = representation.valueForKeyPath("id") as? Int {
            self.id = id
        } else {
            self.id = 0
        }

        if let facebookUID = representation.valueForKeyPath("facebook_UID") as? String {
            self.facebookUID = facebookUID
        }

        if let email = representation.valueForKeyPath("email") as? String {
            self.email = email
        } else {
            self.email = ""
        }

        if let firstName = representation.valueForKeyPath("first_name") as? String {
            self.firstName = firstName
        } else {
            self.firstName = ""
        }

        if let lastName = representation.valueForKeyPath("last_name") as? String {
            self.lastName = lastName
        } else {
            self.lastName = ""
        }

        if let phone = representation.valueForKeyPath("phone") as? String {
            self.phone = phone
        }

        if let position = representation.valueForKeyPath("position_name") as? String {
            self.position = position
        }

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

我的问题是,这种风格解码 JSON 并设置非可选的实例变量的最佳方法?例如,在此声明︰

if let id = representation.valueForKeyPath("id") as? Int {
    self.id = id
}

我很需要由编译器添加 else 子句并设置 id 的东西否则为 xCode 抛出错误说法︰ self.id is not initialized at implicitly generated super.init call

但同时,初始化 self.id 的值 0 是错误的我根本没有帮助。

解决方法 1:

但同时,初始化值为 0 self.id 是错误的我根本没有帮助。

如果有一个默认值为 self.id 感觉不对,那么你应该此属性可选。这样你就不必添加 else 子句︰

final class User: NSObject, ResponseObjectSerializable {
    var id: Int?
    var facebookUID: String?
    var email: String
    var firstName: String
    var lastName: String
    var phone: String?
    var position: String?
    var timeCreated: CVDate

    init?(response: NSHTTPURLResponse, var representation: AnyObject) {
        if let dataRepresentation = ((representation as! NSDictionary).valueForKey("data") as? [String: AnyObject]) {
            representation = dataRepresentation
        }

        if let id = representation.valueForKeyPath("id") as? Int {
            self.id = id
        }

        ...

更新

你在评论中说︰

我总是需要虽然有用户对象的 id。

如果你必须有这 id 属性然后问题是没有实际意义,你只需要做

let id = representation.valueForKeyPath("id") as! Int 

并保证较早,此值将会存在。

因为如果你对象需要一个 ID,然后你无法初始化它无论如何如果此值不存在如果你不想要一个默认值。

官方微信
官方QQ群
31647020