我试图从Swift中的JSON文件中读取并在尝试返回我的数组时继续为nil。
这是JSON文件:
{
"wordList" : {
"difficulty" : "Easy",
"list" : [
"fireplace",
"apple",
"january",
"tooth",
"cookies",
"mysterious",
"essential",
"magenta",
"darling",
"pterodactyl"
]
}}
我试图用以下函数解析它:
func parseJSONForArrayOfWords(filepath filepath: String) -> NSArray? {
let bundle = NSBundle.mainBundle()
let path = bundle.pathForResource(filepath, ofType: "json")!
let jsonData: NSData = NSData(contentsOfFile: path)!
do {
let JSONDict = try NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions(rawValue: 0)) as? NSDictionary
if let dictionary = JSONDict {
if let listWords = dictionary["wordList"] as? NSArray {
return listWords
}
}
} catch let error as NSError {
print("\(error)")
}
return nil
}
其中“list”是我想要返回的数组。我一直都没事。如果有人能帮助我,我将非常感激。谢谢!