因此,使用我是Alamofire和SwiftyJSON,我能够从JSON源中提取数据并将其分配给tableview . 我现在需要做的是按日期过滤结果 . 想法是 grab 今天的日期,只显示等于或大于今天日期的项目 .
现在当我查询源代码时使用Alamofire我得到以下内容
{
"mlb_schedule": {
"columns": [
"awayTeam",
"homeTeam",
"date",
"time"
],
"records": [
[
"TOR",
"BOS",
"7/14/18",
""
],
[
"KCA",
"CHA",
"7/14/18",
""
],
[
"TBA",
"MIN",
"7/14/18",
""
],
[
"MIL",
"PIT",
"7/14/18",
""
],
[
"ARI",
"ATL",
"7/14/18",
""
],
我创建了一个getDate函数来拉取当前日期并发送 . 它在下面的代码中的var dateToday . 我的问题存在于Alamofire中,我似乎无法选择要通过响应过滤的dict值,如下面的代码所示 .
var arrRes = [Any]()
public var baseballSelectedRow: [String] = []
var dateToday = ""
override func viewDidLoad() {
super.viewDidLoad()
getDate()
//Alamofire
Alamofire.request("http://52.34.250.4/mlb/api.php/mlb_schedule").responseJSON { (responseData) -> Void in
if((responseData.result.value) != nil) {
let swiftyJsonVar = JSON(responseData.result.value!)
if let resData = swiftyJsonVar["mlb_schedule"]["records"].arrayObject {
self.arrRes = resData.filter { $0.dict[2] <= dateToday}
}
if self.arrRes.count > 0 {
self.tableView.reloadData()
}
}
}
}
func getDate() {
let currentDate = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .short
let convertableDate = dateFormatter.string(from: currentDate)
dateToday = convertableDate
}
} extension SwarmBaseballViewController:UITableViewDataSource,UITableViewDelegate {func tableView(_ tableView:UITableView,numberOfRowsInSection section:Int) - > Int
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "SwarmListTableViewCell") as? SwarmListTableViewCell {
if let dict = arrRes[indexPath.row] as? [Any] {
if dict.count > 2 {
cell.awayLabel?.text = dict[0] as? String
cell.homeLabel?.text = dict[1] as? String
cell.dateLabel?.text = dict[2] as? String
cell.timeLabel?.text = dict[3] as? String
}
}
return cell
} else {
return SwarmListTableViewCell()
}
}
我意识到我的语法错了但是,不能为我的生活找到/为什么?