Beth.larson1977 Beth.larson1977 - 7 months ago 25
Swift Question

Type ‘Any’ has no subscript members

I'm working through a tutorial and have run into a roadblock with the type any error. I was able to get the others resolved but this one is kicking my tail.

Here is the viewcontroller code:

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.
}


}


The error is thrown on the following lines:
//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!

Any help would be appreciative.

Answer Source

Replace all instances of

teams[i][...]

with:

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