从 NSMutableDictionary 到字符串数组的 Alamofire 结果:Anyobject
Posted
技术标签:
【中文标题】从 NSMutableDictionary 到字符串数组的 Alamofire 结果:Anyobject【英文标题】:Alamofire result from NSMutableDictionary to Array of String : Anyobject 【发布时间】: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 类的实例。帮忙?
【问题讨论】:
【参考方案1】:您可以使用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
【讨论】:
【参考方案2】:将representation
转换为[String: AnyObject]
let representation = representation as? [String: AnyObject]
【讨论】:
以上是关于从 NSMutableDictionary 到字符串数组的 Alamofire 结果:Anyobject的主要内容,如果未能解决你的问题,请参考以下文章
无法从 NSMutableDictionary iOS 应用程序中检索 XMPP JID。
Objective-C:无法从 NSUserDefaults 设置 NSMutableDictionary
使用 nsmutabledictionary 创建嵌套的 JSON 字符串