我正在进行JSON获取,然后我处理数据。创建了我的JSONObject,然后我开始使用数据。可在此处查看示例:https://openlibrary.org/api/books?bibkeys=1593243987&f&jscmd=data&format=json
我提取作者姓名的第一个块工作正常,但第二个将封面网址提取为字符串甚至没有运行,我不明白为什么。
如果我在if let thumbs = bookDictionary["cover"] as? NSArray {
处设置断点,它会停止,但是当我逐步执行代码时,它会跳到最后并继续前进,甚至不会在块中运行任何内容。
我很感激任何人都可以提供帮助。我正在使用Swift 2.0 / Xcode 7b6。
let requestURL = ("https://openlibrary.org/api/books?bibkeys=" + lookUpID + "&f&jscmd=data&format=json")
let url = NSURL(string: requestURL)
let req = NSURLRequest(URL: url!)
let dataTask = session.dataTaskWithRequest(req) {
(data, response, error) in
do {
let jsonObject = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary
if let bookDictionary: AnyObject = jsonObject!["\(self.lookUpID)"] {
// Retrieve the author name
var names = [String]()
if let authors = bookDictionary["authors"] as? NSArray {
for author in authors {
if let author = author as? NSDictionary,
let name = author["name"] as? String {
names.append(name)
}
}
}
// Retrieve cover url
var coverThumbURL: String = ""
if let thumbs = bookDictionary["cover"] as? NSArray {
// This code isn't running at all.
for thumb in thumbs {
if let thumb = thumb as? NSDictionary,
let thumbnail = thumb["medium"] as? String {
coverThumbURL = thumbnail
}
}
}
}
答案 0 :(得分:1)
感谢您的帮助。我做了一些环顾四周&固定铸件。
var coverThumbURL: String = ""
if let thumbs = bookDictionary["cover"] as? NSDictionary {
let thumbnail = thumbs.valueForKey("medium") as? String
coverThumbURL = thumbnail!
}