gpt4 book ai didi

php - 如何在 Swift 中访问从 PHP 传递的数组中的数据?

转载 作者:行者123 更新时间:2023-11-30 12:42:56 25 4
gpt4 key购买 nike

我无法访问此数组中 Form 的内容:

["leagueStats": {
Form = (
(
{
date = "2017-01-31";
"player1_name" = Dicky;
"player1_result" = L;
"results_id" = 42;
},
{
date = "2017-01-26";
"player1_name" = Dicky;
"player1_result" = L;
"results_id" = 41;
},
{
date = "2017-01-24";
"player1_name" = Dicky;
"player1_result" = D;
"results_id" = 37;
},
{
date = "2017-01-21";
"player1_name" = Dicky;
"player1_result" = W;
"results_id" = 34;
},
{
date = "2017-01-21";
"player1_name" = Dicky;
"player1_result" = L;
"results_id" = 35;
},
{
date = "2017-01-07";
"player1_name" = Dicky;
"player1_result" = W;
"results_id" = 27;
},
{
date = "2016-12-30";
"player1_name" = Dicky;
"player1_result" = W;
"results_id" = 24;
},
{
date = "2016-12-24";
"player1_name" = Dicky;
"player1_result" = W;
"results_id" = 21;
}
),
(
{
date = "2017-01-25";
"player1_name" = G;
"player1_result" = D;
"results_id" = 40;
},
{
date = "2017-01-25";
"player1_name" = G;
"player1_result" = W;
"results_id" = 39;
},
{
date = "2017-01-25";
"player1_name" = G;
"player1_result" = W;
"results_id" = 38;
},
{
date = "2017-01-24";
"player1_name" = G;
"player1_result" = D;
"results_id" = 37;
},
{
date = "2017-01-24";
"player1_name" = G;
"player1_result" = W;
"results_id" = 36;
},
{
date = "2017-01-21";
"player1_name" = G;
"player1_result" = W;
"results_id" = 35;
},
{
date = "2017-01-21";
"player1_name" = G;
"player1_result" = L;
"results_id" = 34;
},
{
date = "2017-01-21";
"player1_name" = G;
"player1_result" = D;
"results_id" = 33;
}
),
(
{
date = "2017-01-31";
"player1_name" = Sultan;
"player1_result" = W;
"results_id" = 42;
},
{
date = "2017-01-26";
"player1_name" = Sultan;
"player1_result" = W;
"results_id" = 41;
},
{
date = "2017-01-25";
"player1_name" = Sultan;
"player1_result" = L;
"results_id" = 38;
},
{
date = "2017-01-25";
"player1_name" = Sultan;
"player1_result" = D;
"results_id" = 40;
},
{
date = "2017-01-25";
"player1_name" = Sultan;
"player1_result" = L;
"results_id" = 39;
},
{
date = "2017-01-24";
"player1_name" = Sultan;
"player1_result" = L;
"results_id" = 36;
},
{
date = "2017-01-21";
"player1_name" = Sultan;
"player1_result" = D;
"results_id" = 33;
},
{
date = "2017-01-15";
"player1_name" = Sultan;
"player1_result" = L;
"results_id" = 30;
}
)
);
]

这是我的代码:

 override func viewDidAppear(_ animated: Bool) {

let myUrl = URL(string: "http://www.xyz.uk/xyz/getLeagueStats.php")
var request = URLRequest(url:myUrl!)
request.httpMethod = "POST"

let postString = "player_id=\(self.playerId2!)&community_id=\(communityId2!)";

request.httpBody = postString.data(using: String.Encoding.utf8);
let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
DispatchQueue.main.async
{
do{

let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject]
print (json!)

if let dict = json?["leagueStats"] as? [String:AnyObject] {
print ("step 1")

if let arr = dict["Form"] as? [[String:AnyObject]] {

print ("step 2")

self.leagueForm = arr.flatMap { Form($0 as! [String : String]) }
print (self.leagueForm)
print (self.leagueForm.count)

for i in 0..<self.leagueForm.count {
let form = self.leagueForm[i]
print (form.player_results!)
self.formGuide.append(form.player_results!)

}

print ("now")
print (self.formGuide)
self.resultsDisplay.results = self.formGuide
self.resultsDisplay.results = self.resultsDisplay.results.reversed()
self.resultsDisplay.displayResults()

}

}

数组 Form 位于数组 leagueStats 内,因为 leagueStats 还包含来自 PHP API 调用的其他数组。

Form内应该有3个数组,结果数据为;迪基,G,苏丹。

我无法访问这些数据是哪里出了问题?

我不确定是否需要,但这是我处理此调用的 PHP 脚本:

$communityPlayersIds = array();
$communityPlayersIds = $dao->getSpecificCommunity($communityId);
foreach ($communityPlayersIds as $cPI){
$playerForm[] = $dao->getCommunityForm($cPI, $communityId);
$i++;
}
$Form = array('playerForm' => $playerForm);
$arr = array('Form' => $playerForm);
echo json_encode (array('leagueStats' => $arr));

本文顶部的输出来自 print(json!)

我正在打印“step1”,但代码此时失败,因为“step2”未打印。

最佳答案

您无需评估所有 24 个字典对象即可了解问题所在。精简数据,以便更容易理解其真正的格式。这基本上就是您所拥有的:

["leagueStats": {
Form = (
(
{
date = "2017-01-31";
"player1_name" = Dicky;
"player1_result" = L;
"results_id" = 42;
},
{
date = "2016-12-24";
"player1_name" = Dicky;
"player1_result" = W;
"results_id" = 21;
}
),
(
{
date = "2017-01-31";
"player1_name" = Sultan;
"player1_result" = W;
"results_id" = 42;
},
{
date = "2017-01-15";
"player1_name" = Sultan;
"player1_result" = L;
"results_id" = 30;
}
)
);
]

所以你真的有:

key : {
key : {
[dictionary, dictionary],
[dictionary, dictionary]
}
}

编辑:

if let arr = dict["Form"] as? [[String:AnyObject]]

指示“Form”的值应该是字典数组。您有一个字典数组的数组。

关于php - 如何在 Swift 中访问从 PHP 传递的数组中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42030010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com