私はトラブルこの配列内Form
の内容へのアクセスを持っています:Swiftでこの配列のデータにアクセスするには、どうすればPHPから渡されますか?
["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()
}
}
leagueStats
もAから他の配列を運ぶための配列Form
が配列leagueStats
内にありますPHP API呼び出し。
Form
に3つの配列があります。ディッキー、G、スルタン。
このデータにアクセスできないと私は間違っていますか?
私はそれが必要なのかはわからないが、ここで、このコールを扱う私のPHPスクリプトです:この記事の上部にある出力がprint(json!)
からである
$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));
。
私は 'step1'を印刷していますが、 'step2'は印刷されていないので、その時点でコードが失敗しています。
フランキーありがとうございました。私はスウィフトにかなり新しいので、私がする必要があることに関するいくつかの指示で拡張していただけますか?あなたがしていることを完全に理解していない。乾杯 – RDowns
私は自分の答えに編集を加えました。 – Frankie