类型“任何”没有下标成员[重复]

Posted

技术标签:

【中文标题】类型“任何”没有下标成员[重复]【英文标题】:Type ‘Any’ has no subscript members [duplicate] 【发布时间】:2017-05-11 19:02:53 【问题描述】:

我正在学习教程,但遇到了任何类型错误的障碍。我能够解决其他问题,但这个问题正在踢我的尾巴。

这是视图控制器代码:

import UIKit

class ViewController: UIViewController 

//Our web service url 
let URL_GET_TEAMS:String = "http://192.168.43.207/MyWebService/api/getteams.php"

override func viewDidLoad() 
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.


    //created NSURL
    let requestURL = NSURL(string: URL_GET_TEAMS)


    //creating NSMutableURLRequest
    let request = NSMutableURLRequest(URL: requestURL!)

    //setting the method to post
    request.HTTPMethod = "GET"

    //creating a task to send the post request
    let task = NSURLSession.sharedSession().dataTaskWithRequest(request)
        data, response, error in

        //exiting if there is some error
        if error != nil
            print("error is \(error)")
            return;
        

        //parsing the response
        do 
            //converting resonse to NSDictionary
            var teamJSON: NSDictionary!
            teamJSON =  try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary

            //getting the JSON array teams from the response
            let teams: NSArray = teamJSON["teams"] as! NSArray

            //looping through all the json objects in the array teams
            for i in 0 ..< teams.count

                //getting the data at each index
                let teamId:Int = teams[i]["id"] as! Int!
                let teamName:String = teams[i]["name"] as! String!
                let teamMember:Int = teams[i]["member"] as! Int!

                //displaying the data
                print("id -> ", teamId)
                print("name -> ", teamName)
                print("member -> ", teamMember)
                print("===================")
                print("")

            

         catch 
            print(error)
        
    
    //executing the task
    task.resume()



override func didReceiveMemoryWarning() 
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.




以下行引发错误: //获取每个索引处的数据 让 teamId:Int = teams[i]["id"] as!诠释! 让 teamName:String = teams[i]["name"] as!细绳! 让 teamMember:Int = teams[i]["member"] as!诠释!

任何帮助将不胜感激。

【问题讨论】:

【参考方案1】:

替换

的所有实例
teams[i][...]

与:

(teams[i] as! NSDictionary)[...] 

【讨论】:

谢谢!成功了!

以上是关于类型“任何”没有下标成员[重复]的主要内容,如果未能解决你的问题,请参考以下文章

为firebase数据库创建用户对象时,类型“任何”没有下标成员[重复]

输入“任何?”没有下标成员[重复]

在 swift 3 中打印 json 时出现任何下标成员错误 [重复]

输入“任何?”没有下标成员[重复]

类型“Any”没有下标成员/Swift 3 [重复]

类型“any”在alamofire 4 iOS swift中没有下标成员[重复]