我有一个输出以下内容的数组:
[L,W,L,W,L,L,W,W]
这是正确的值,但我希望它反转,所以它读取
[W,W,L,L,W,L,W,L]
这是我的代码:
for value in forms {
if let arr = [value] as? [[String:Any]] {
self.leagueForm = arr.flatMap { Form($0) }
for form in self.leagueForm {
self.formGuide.append(form.player_result!)
self.formGuide = self.formGuide.reversed()
}
}
}
print ("update" ,self.formGuide)
如果没有第self.formGuide = self.formGuide.reversed()
行,则它具有正确的值但顺序错误,使用此行将输出完全更改为:
[W,L,W,W,L,L,L,W]
这完全不正确???
更新:按要求提供信息:
[KeepScore.ShowCommunityViewController.Form(player_result: Optional("L"), player_name: Optional("Dicky"), result_date: Optional("2017-02-23"), result_id: Optional("93"))]
[KeepScore.ShowCommunityViewController.Form(player_result: Optional("W"), player_name: Optional("Dicky"), result_date: Optional("2017-02-23"), result_id: Optional("90"))]
[KeepScore.ShowCommunityViewController.Form(player_result: Optional("L"), player_name: Optional("Dicky"), result_date: Optional("2017-02-23"), result_id: Optional("89"))]
[KeepScore.ShowCommunityViewController.Form(player_result: Optional("W"), player_name: Optional("Dicky"), result_date: Optional("2017-02-23"), result_id: Optional("88"))]
[KeepScore.ShowCommunityViewController.Form(player_result: Optional("L"), player_name: Optional("Dicky"), result_date: Optional("2017-02-22"), result_id: Optional("57"))]
[KeepScore.ShowCommunityViewController.Form(player_result: Optional("L"), player_name: Optional("Dicky"), result_date: Optional("2017-02-22"), result_id: Optional("56"))]
[KeepScore.ShowCommunityViewController.Form(player_result: Optional("W"), player_name: Optional("Dicky"), result_date: Optional("2017-02-22"), result_id: Optional("55"))]
[KeepScore.ShowCommunityViewController.Form(player_result: Optional("W"), player_name: Optional("Dicky"), result_date: Optional("2017-02-22"), result_id: Optional("54"))]`
更新的代码:
if let forms = json?["playerForm"] as? [[String: Any]] {
print (forms)
for value in forms {
if let arr = [value] as? [[String:Any]] {
self.leagueForm = arr.flatMap { Form($0) }
self.formGuide = self.leagueForm.flatMap { $0.player_result }.reversed()
}
}
print ("update" ,self.formGuide)
答案 0 :(得分:3)
我认为你需要
self.formGuide = forms.reversed()
答案 1 :(得分:2)
问题是你在for循环中使数组反转,你在数组中添加对象,这是你遇到这个问题的原因。
要解决这个问题,只需在reversedArray = reversedArray.reversed()
之后放置行for loop
或避免使用for loop
并使用flatMap
,就像用来创建字典数组中的对象数组一样
self.formGuide = self.leagueForm.flatMap { $0.player_result }.reversed()
修改:您正在if if中添加for loop
,并且没有必要。
if let forms = json?["playerForm"] as? [[String: Any]] {
self.leagueForm = forms.flatMap { Form($0) }
self.formGuide = self.leagueForm.flatMap { $0.player_result }.reversed()
}
答案 2 :(得分:2)
您只需致电
即可self.formGuide.reverse()
您希望以相反的顺序访问数组,例如在您的进一步代码执行中或存储它。只是不要在你向数组中添加东西的循环中调用它。
答案 3 :(得分:1)
您可以保留第一个代码示例,然后将self.formGuide = self.formGuide.reversed()
放在for loop
for value in forms {
if let arr = [value] as? [[String:Any]] {
self.leagueForm = arr.flatMap { Form($0) }
for form in self.leagueForm {
self.formGuide.append(form.player_result!)
}
}
}
self.formGuide = self.formGuide.reversed()
print ("update" ,self.formGuide)