【发布时间】:2017-04-05 16:10:19
【问题描述】:
我在 Swift2 中编写了自己的函数来解析 JSON。解析 JSON 后,从 JSON 中提取的数据列表将显示在我的应用程序上的 tableView 中。我试图弄清楚如何按字母顺序显示这些数据。我认为这需要在我在函数中调用 append 方法之前的某个地方发生。我想这需要是一个sort 函数,但我无法在 Swift2 中找出正确的排序函数来正确执行它。感谢我能得到的任何帮助!
这是我的 parseJSON 函数:
func parseJSON(){
do{
let data = NSData(contentsOfURL: NSURL(string: "https://jsonblob.com/api/jsonBlob/580d0ccce4b0bcac9f837fbe")!)
let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)
for anItem in jsonResult as! [Dictionary<String, AnyObject>]{
let mifiName2 = anItem["name"] as! String
let mifiId = anItem["employeeId"] as! Int
let newName = Name(mifiName: mifiName2, mifiId: mifiId)
nameOfMifi.append(newName)
//print("Name: \(newName)")
}
}
catch let error as NSError{
print(error.debugDescription)
}
}
【问题讨论】:
-
从不使用同步方法
NSData(contentsOfURL:和MutableContainers从服务器加载数据在Swift中是没有用的。
标签: ios json swift sorting swift2