从 NSMutableDictionary 到字符串数组的 Alamofire 结果:Anyobject
Alamofire result from NSMutableDictionary to Array of String : Anyobject
我正在通过 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/ 我的主题 class 具有以下方法:
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 数据在 "data" 而不是根级对象有关,但我不知道如何告诉演员拉出 "data" 元素来制作Subject 的实例 class。帮忙?
将 representation
转换为 [String: AnyObject]
let representation = representation as? [String: AnyObject]
您可以使用 valueForKeyPath
提取 data
元素:
static func collection(response response: NSHTTPURLResponse, representation: AnyObject) -> [Subject] {
var subjects: [Subject] = []
if let representation = representation.valueForKeyPath("data") as? [[String: AnyObject]] {
for subjectRepresentation in representation {
if let subject = Subject(response: response, representation: subjectRepresentation) {
subjects.append(subject)
}
}
}
return subjects
}
我正在通过 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/ 我的主题 class 具有以下方法:
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 数据在 "data" 而不是根级对象有关,但我不知道如何告诉演员拉出 "data" 元素来制作Subject 的实例 class。帮忙?
将 representation
转换为 [String: AnyObject]
let representation = representation as? [String: AnyObject]
您可以使用 valueForKeyPath
提取 data
元素:
static func collection(response response: NSHTTPURLResponse, representation: AnyObject) -> [Subject] {
var subjects: [Subject] = []
if let representation = representation.valueForKeyPath("data") as? [[String: AnyObject]] {
for subjectRepresentation in representation {
if let subject = Subject(response: response, representation: subjectRepresentation) {
subjects.append(subject)
}
}
}
return subjects
}