【发布时间】:2015-10-08 04:22:25
【问题描述】:
我正在通过 alamofire 发出请求,并收到此响应:
{
success: true,
data: [
{
_id: "5615dd59e4b0d2d408b385ff",
name: "Exam Prep",
info: "Tools designed to increase students' performance on standardized tests",
active: true
},
{
_id: "5615dd75e4b0d2d408b38603",
name: "Mathematics",
info: "The study of topics such as quantity numbers, structure, space, and change",
active: true
},
{
_id: "5615dd8de4b0d2d408b38604",
name: "Science",
info: "Knowledge in the form of predictions about the universe",
active: true
},
{
_id: "5615dda5e4b0d2d408b38605",
name: "Language Arts",
info: "The study of languages, composition, and grammar",
active: true
}
]
}
使用通用对象序列化作为模板形成 Alamofire 的文档:http://cocoadocs.org/docsets/Alamofire/2.0.2/ 我的主题类具有以下方法:
static func collection(response response: NSHTTPURLResponse, representation: AnyObject) -> [Subject] {
var subjects: [Subject] = []
if let representation = representation as? [[String: AnyObject]] {
for subjectRepresentation in representation {
if let subject = Subject(response: response, representation: subjectRepresentation) {
subjects.append(subject)
}
}
}
return subjects
}
但是 if let representation = representation as 的演员表? [[String: AnyObject]] 总是失败。我相信这与“数据”而不是根级别对象中的 json 数据有关,但我不知道如何告诉演员拉出“数据”元素以创建 Subject 类的实例。帮忙?
【问题讨论】:
标签: swift nsmutabledictionary alamofire