我有一个JSON获取发生,然后我做的东西与数据。创建了JSONObject,然后开始处理数据。这里可以看到一个样本:https://openlibrary.org/api/books?bibkeys=1593243987&f&jscmd=data&format=json
我的第一个提取作者姓名的块工作得很好,但是第二个提取封面url作为字符串的块甚至没有运行,我不知道为什么。
如果我在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
}
}
}
}
最佳答案
谢谢你的帮助。我四处看了看,把铸件修好了。
var coverThumbURL: String = ""
if let thumbs = bookDictionary["cover"] as? NSDictionary {
let thumbnail = thumbs.valueForKey("medium") as? String
coverThumbURL = thumbnail!
}