热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

元组类型'(键:字符串,值:AnyObject)'的值没有成员'下标'

如何解决《元组类型'(键:字符串,值:AnyObject)'的值没有成员'下标'》经验,为你挑选了1个好方法。

我收到这样的错误:元组类型'(键:字符串,值:AnyObject)'的值没有成员'下标'

我试图在线搜索,但我不明白,它总是说一些将其更改为字典数组的方法,但是当我将数据解析为[[String:AnyObject]]时,它给我一个错误。

错误截图

这是我的上下文代码

`//
//  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()
    }


`

小智.. 5

来自Apple文档:您可以使用for-in循环在字典中的键/值对之间进行迭代。字典中的每个项目都作为(键,值)元组返回,并且您可以在迭代过程中将元组的成员分解为临时常量或变量:

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

并注意代码的另一个问题:1)函数getLocation()中有异步代码,当您在viewDidLoad()中赋值时

let locatiOns= appDelegate.locationData

位置等于零。

2)Swift 4具有解析JSON的有用功能,对此进行研究。现在,您将获得仅包含一对键值等于“结果”的键值对的字典



1> 小智..:

来自Apple文档:您可以使用for-in循环在字典中的键/值对之间进行迭代。字典中的每个项目都作为(键,值)元组返回,并且您可以在迭代过程中将元组的成员分解为临时常量或变量:

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

并注意代码的另一个问题:1)函数getLocation()中有异步代码,当您在viewDidLoad()中赋值时

let locatiOns= appDelegate.locationData

位置等于零。

2)Swift 4具有解析JSON的有用功能,对此进行研究。现在,您将获得仅包含一对键值等于“结果”的键值对的字典


推荐阅读
author-avatar
手机用户2502908935
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有