Swift 带有 SwiftyJSON 的失败初始化器
Swift Failable Initializer with SwiftyJSON
我正在尝试用 SwiftyJSON
中的一些 JSON 初始化一个简单的数据模型对象。如果任何必需的 JSON 值不存在,我希望初始化程序失败并且 return nil。这是我的代码:
class ProductCategory: NSObject {
let id: String
let sortOrder: Int
let name: String
let imageURL: String
let ranges: [String]
init?(json: JSON) {
if let jsonID = json["id"].string,
jsonSortOrder = json["sortOrder"].int,
jsonName = json["name"].string,
jsonImageURL = json["imageURL"].string {
id = jsonID
sortOrder = jsonSortOrder
name = jsonName
imageURL = jsonImageURL
ranges = json["ranges"].arrayValue.map { [=11=].string! }
} else {
return nil
}
}
}
我希望这能奏效。如果我们没有达到所有这些 json 值,只需 return nil 并退出。但是,我在 return nil
上收到一条错误消息:
All stored properties of a class instance must be initialized before
returning nil from an initializer.
我很困惑:如果出现问题,我可以在不设置它的情况下摆脱失败初始化器的意义吗? returned 的对象将是 nil
,为什么设置它的属性会有任何价值?
Failable Initializers for Classes:
"For classes, however, a failable initializer can trigger an initialization failure only after all stored properties introduced by that class have been set to an initial value and any initializer delegation has taken place."
所以
init?(json: JSON) {
self.id = json["id"].string
self.sortOrder = json["sortOrder"].int
...
if ... { return nil }
}
所以这就是我最终做的事情 – Greg 是对的,但我最终切换到 struct
结果是:
struct ProductCategory {
let id: String
let sortOrder: Int
let name: String
let imageURL: String
let ranges: [String]
init?(json: JSON) {
guard let jsonID = json["id"].string,
let jsonSortOrder = json["sortOrder"].int,
let jsonName = json["name"].string,
let jsonImageURL = json["image"].string else {
return nil
}
self.id = jsonID
self.sortOrder = jsonSortOrder
self.name = jsonName
self.imageURL = jsonImageURL
self.ranges = json["ranges"].arrayValue.map { [=10=].string! }
}
}
我正在尝试用 SwiftyJSON
中的一些 JSON 初始化一个简单的数据模型对象。如果任何必需的 JSON 值不存在,我希望初始化程序失败并且 return nil。这是我的代码:
class ProductCategory: NSObject {
let id: String
let sortOrder: Int
let name: String
let imageURL: String
let ranges: [String]
init?(json: JSON) {
if let jsonID = json["id"].string,
jsonSortOrder = json["sortOrder"].int,
jsonName = json["name"].string,
jsonImageURL = json["imageURL"].string {
id = jsonID
sortOrder = jsonSortOrder
name = jsonName
imageURL = jsonImageURL
ranges = json["ranges"].arrayValue.map { [=11=].string! }
} else {
return nil
}
}
}
我希望这能奏效。如果我们没有达到所有这些 json 值,只需 return nil 并退出。但是,我在 return nil
上收到一条错误消息:
All stored properties of a class instance must be initialized before returning nil from an initializer.
我很困惑:如果出现问题,我可以在不设置它的情况下摆脱失败初始化器的意义吗? returned 的对象将是 nil
,为什么设置它的属性会有任何价值?
Failable Initializers for Classes:
"For classes, however, a failable initializer can trigger an initialization failure only after all stored properties introduced by that class have been set to an initial value and any initializer delegation has taken place."
所以
init?(json: JSON) {
self.id = json["id"].string
self.sortOrder = json["sortOrder"].int
...
if ... { return nil }
}
所以这就是我最终做的事情 – Greg 是对的,但我最终切换到 struct
结果是:
struct ProductCategory {
let id: String
let sortOrder: Int
let name: String
let imageURL: String
let ranges: [String]
init?(json: JSON) {
guard let jsonID = json["id"].string,
let jsonSortOrder = json["sortOrder"].int,
let jsonName = json["name"].string,
let jsonImageURL = json["image"].string else {
return nil
}
self.id = jsonID
self.sortOrder = jsonSortOrder
self.name = jsonName
self.imageURL = jsonImageURL
self.ranges = json["ranges"].arrayValue.map { [=10=].string! }
}
}