gpt4 book ai didi

ios - 在 Swift 中处理解析 JSON 时出错

转载 作者:行者123 更新时间:2023-11-28 13:05:02 25 4
gpt4 key购买 nike

我正在使用 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 未在隐式生成的 super.init 调用中初始化

但与此同时,用 0 值初始化 self.id 是错误的,对我一点帮助都没有。

最佳答案

But at the same time, intializing self.id with a value of 0 is wrong and doesn't help me at all.

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

...

更新

你在评论中说:

I always need to have an id for the user object though.

如果您必须拥有此 id 属性,那么这个问题就没有实际意义了,您只需要做

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

并保证及早该值将存在。

因为如果你的对象需要一个ID,那么你无论如何都不能初始化它如果这个值不存在并且如果你不需要默认值。

关于ios - 在 Swift 中处理解析 JSON 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33059524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com