2017-02-03 6 views
0

私はトラブルこの配列内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'は印刷されていないので、その時点でコードが失敗しています。

答えて

0

あなたは間違っているかを理解するために、すべての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'の値が辞書の配列であることを示します。あなたは辞書の配列の配列を持っています。

+0

フランキーありがとうございました。私はスウィフトにかなり新しいので、私がする必要があることに関するいくつかの指示で拡張していただけますか?あなたがしていることを完全に理解していない。乾杯 – RDowns

+0

私は自分の答えに編集を加えました。 – Frankie

関連する問題