2017-11-14 9 views
0

タプルタイプ '下付き文字 'タプルタイプ '(キー:文字列、値:AnyObject)'の値にメンバのサブスクリプトがありません

私はオンラインで検索しようとしましたが、私は理解できません。それは辞書の配列に変更することを常に言いますが、[[String:AnyObject]]として解析するとエラーです。ここで

Error Screenshot

アップルのドキュメントからコンテキスト

`// 
// MapViewViewController.swift 
// On the Map! 
// 
// Created by Belal Elsiesy on 11/13/17. 
// Copyright © 2017 Elsiesy Industries. All rights reserved. 
// 

import UIKit 

import MapKit 

class MapViewViewController: UIViewController, MKMapViewDelegate { 

    @IBOutlet weak var MapView: MKMapView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 
     getLocations() 
     let appDelegate = UIApplication.shared.delegate as! AppDelegate 

     let locations = appDelegate.locationData 
     var annotations = [MKPointAnnotation]() 

     // When the array is complete, we add the annotations to the map. 

     for location in locations! { 

      // Notice that the float values are being used to create CLLocationDegree values. 
      // This is a version of the Double type. 
      let lat = CLLocationDegrees(location["latitude"] as! Double) 
      let long = CLLocationDegrees(location["longitude"] as! Double) 

      // The lat and long are used to create a CLLocationCoordinates2D instance. 
      let coordinate = CLLocationCoordinate2D(latitude: lat, longitude: long) 

      let first = location["firstName"] as! String 
      let last = location["lastName"] as! String 
      let mediaURL = location["mediaURL"] as! String 

      // Here we create the annotation and set its coordiate, title, and subtitle properties 
      let annotation = MKPointAnnotation() 
      annotation.coordinate = coordinate 
      annotation.title = "\(first) \(last)" 
      annotation.subtitle = mediaURL 

      // Finally we place the annotation in an array of annotations. 
      annotations.append(annotation) 
     } 
     // When the array is complete, we add the annotations to the map. 
     self.MapView.addAnnotations(annotations) 
    } 
} 




    /* 
    // MARK: - Navigation 

    // In a storyboard-based application, you will often want to do a little preparation before navigation 
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     // Get the new view controller using segue.destinationViewController. 
     // Pass the selected object to the new view controller. 
    } 
    */ 
func getLocations() { 

     var request = URLRequest(url: URL(string: "https://parse.udacity.com/parse/classes/StudentLocation")!) 
     request.addValue("QrX47CA9cyuGewLdsL7o5Eb8iug6Em8ye0dnAbIr", forHTTPHeaderField: "X-Parse-Application-Id") 
     request.addValue("QuWThTdiRmTux3YaDseUSEpUKo7aBYM737yKd4gY", forHTTPHeaderField: "X-Parse-REST-API-Key") 
     let session = URLSession.shared 
     let task = session.dataTask(with: request) { data, response, error in 
      if error != nil { // Handle error... 
       ////////////////////////DO THIS LATER 
      } 
      print(String(data: data!, encoding: .utf8)!) 
     let parsedResult: [String:AnyObject]! 
      do { 
       parsedResult = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String : AnyObject] 


       let appDelegate = UIApplication.shared.delegate as! AppDelegate 
       appDelegate.locationData = parsedResult 
      } catch { 
       print("Could not parse the data as JSON: '\(data)'") 

      } 


     } 
     task.resume() 
    } 


` 
+0

ロケーション変数には添字がないため、[]を使用して情報を照会することはできません。私はあなたの可変場所のタイプについて誤解があると思います。これはタプル(String、Anyオブジェクト)であり、辞書[String:AnyObject]ではありません。 – Arrabidas92

答えて

2

ための私のコードです:あなたはのためのインループで辞書内のキーと値のペアを反復することができます。辞書の各項目は(キー、値)タプルとして返され、そしてあなたは、反復の一部として、一時的な定数や変数にタプルのメンバーを分解することができます

for (key, value) in dictionary { 
    print(key) 
    print(value) 
} 

そして、あなたのコードを使用して、他の問題に注意を払います: 1)非同期関数のgetLocation()のコードとあなたがのviewDidLoadに割り当てるときに()

let locations = appDelegate.locationData 

場所等しいnilがあります。

2)Swift 4は、JSONを解析するための便利な機能を備えています。キーと値のペアがキーの等しい "result"のキーを1つだけ持つ辞書を取得しました

+0

どうすればこの問題を解決できますか? –

+0

ただそれを修正する高速な解決策はありません。 asyncを使用する場合は、getLocation(ハンドラ:@escaping:() - >())を使用するか、別の方法を見つけることができます。 JSONでは、別の構造体Locationを使用してデータを保持し、JSONをこの構造体の配列にマップすることをお勧めします。まず、非同期関数を使った作業と、Swift 4のJSONの構文解析についてお読みください。 – FuzzzzyBoy

関連する問題