如何使用 Argo 和 Swift 创建子类?
How can I create subclasses using Argo and Swift?
我在 Swift 应用程序中使用 Argo 将 JSON 解码为对象。我有 JSON 这样的:
"activities": [
{
"id": "intro-to-the-program",
"type": "session",
"audio": "intro-to-the-program.mp3"
},
{
"id": "goal-setting",
"type": "session",
"audio": "goal-setting.mp3"
},
{
"id": "onboarding-quiz",
"type": "quiz"
}
]
基于'type',其实我想实例化一个子class of the Activity class (ActivitySession, Activity测验等)并让 subclass 进行自己的解码。
我该怎么做?顶级 decode() 函数需要 return 类型的 Decoded<Activity>
,而我目前的 none 方法似乎能够击败它。
这是一种方法,您可以通过打开类型来有条件地对其进行解码,并在给出无效类型时收到一条很好的错误消息。
struct ThingWithActivities: Decodable {
let activities: [Activity]
static func decode(json: JSON) -> Decoded<ThingWithActivities> {
return curry(ThingWithActivities.init)
<^> json <|| "activities"
}
}
class Activity: Decodable {
let id: String
init(id: String) {
self.id = id
}
class func decode(json: JSON) -> Decoded<Activity> {
let decodedType: Decoded<String> = json <| "type"
return decodedType.flatMap { type in
switch type {
case "session": return ActivitySession.decode(json)
case "quiz": return ActivityQuiz.decode(json)
default:
return .Failure(.Custom("Expected valid type, found: \(type)"))
}
}
}
}
class ActivitySession: Activity {
let audio: String
init(id: String, audio: String) {
self.audio = audio
super.init(id: id)
}
override static func decode(json: JSON) -> Decoded<Activity> {
return curry(ActivitySession.init)
<^> json <| "id"
<*> json <| "audio"
}
}
class ActivityQuiz: Activity {
override static func decode(json: JSON) -> Decoded<Activity> {
return curry(ActivityQuiz.init)
<^> json <| "id"
}
}
let activities: Decoded<ThingWithActivities>? = JSONFromFile("activities").flatMap(decode)
print(activities)
// => Optional(Success(ThingWithActivities(activities: [ActivitySession, ActivitySession, ActivityQuiz])))
关键部分是提取类型并.flatMap
处理它以有条件地解码成它应该是的类型。
我在 Swift 应用程序中使用 Argo 将 JSON 解码为对象。我有 JSON 这样的:
"activities": [
{
"id": "intro-to-the-program",
"type": "session",
"audio": "intro-to-the-program.mp3"
},
{
"id": "goal-setting",
"type": "session",
"audio": "goal-setting.mp3"
},
{
"id": "onboarding-quiz",
"type": "quiz"
}
]
基于'type',其实我想实例化一个子class of the Activity class (ActivitySession, Activity测验等)并让 subclass 进行自己的解码。
我该怎么做?顶级 decode() 函数需要 return 类型的 Decoded<Activity>
,而我目前的 none 方法似乎能够击败它。
这是一种方法,您可以通过打开类型来有条件地对其进行解码,并在给出无效类型时收到一条很好的错误消息。
struct ThingWithActivities: Decodable {
let activities: [Activity]
static func decode(json: JSON) -> Decoded<ThingWithActivities> {
return curry(ThingWithActivities.init)
<^> json <|| "activities"
}
}
class Activity: Decodable {
let id: String
init(id: String) {
self.id = id
}
class func decode(json: JSON) -> Decoded<Activity> {
let decodedType: Decoded<String> = json <| "type"
return decodedType.flatMap { type in
switch type {
case "session": return ActivitySession.decode(json)
case "quiz": return ActivityQuiz.decode(json)
default:
return .Failure(.Custom("Expected valid type, found: \(type)"))
}
}
}
}
class ActivitySession: Activity {
let audio: String
init(id: String, audio: String) {
self.audio = audio
super.init(id: id)
}
override static func decode(json: JSON) -> Decoded<Activity> {
return curry(ActivitySession.init)
<^> json <| "id"
<*> json <| "audio"
}
}
class ActivityQuiz: Activity {
override static func decode(json: JSON) -> Decoded<Activity> {
return curry(ActivityQuiz.init)
<^> json <| "id"
}
}
let activities: Decoded<ThingWithActivities>? = JSONFromFile("activities").flatMap(decode)
print(activities)
// => Optional(Success(ThingWithActivities(activities: [ActivitySession, ActivitySession, ActivityQuiz])))
关键部分是提取类型并.flatMap
处理它以有条件地解码成它应该是的类型。