类型“任何”没有下标成员

时间:2017-05-11 19:02:54

标签: ios swift xcode

我正在完成一个教程,遇到任何错误类型的包版广告。我能够让其他人得到解决,但是这个人正在踢我的尾巴。

以下是viewcontroller代码:

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”]为!诠释!     让teamName:String = teams [i] [“name”]为!串!     让teamMember:Int = teams [i] [“member”]为! INT!

任何帮助都会很有意义。

1 个答案:

答案 0 :(得分:-3)

替换

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

使用:

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