Ios 类型‘;任何’;没有下标成员
我正在完成一个教程,遇到了一个类型为any error的障碍。我能解决其他问题,但这件事让我很恼火 以下是viewcontroller代码:Ios 类型‘;任何’;没有下标成员,ios,swift,xcode,Ios,Swift,Xcode,我正在完成一个教程,遇到了一个类型为any error的障碍。我能解决其他问题,但这件事让我很恼火 以下是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 view
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.
}
}
导入UIKit
类ViewController:UIViewController{
//我们的web服务url
让URL_获取团队:字符串=”http://192.168.43.207/MyWebService/api/getteams.php"
重写func viewDidLoad(){
super.viewDidLoad()
//加载视图后,通常从nib执行任何其他设置。
//创建NSURL
让requestURL=NSURL(字符串:URL\u GET\u团队)
//创建NSMutableURLRequest
let request=NSMutableURLRequest(URL:requestURL!)
//将方法设置为post
request.HTTPMethod=“GET”
//创建任务以发送post请求
让task=NSURLSession.sharedSession().dataTaskWithRequest(请求){
数据、响应、错误
//如果出现错误,则退出
如果错误!=nil{
打印(“错误为\(错误)”)
返回;
}
//解析响应
做{
//将resonse转换为NSDictionary
var teamJSON:NSDictionary!
teamJSON=尝试将NSJSONSerialization.JSONObjectWithData(data!,选项:.MutableContainers)作为NSDictionary
//从响应中获取JSON数组团队
让团队:NSArray=teamJSON[“团队”]作为!NSArray
//循环遍历阵列团队中的所有json对象
对于0中的i..”,团队id)
打印(“名称->”,团队名称)
打印(“成员->”,团队成员)
打印(“===============================”)
打印(“”)
}
}抓住{
打印(错误)
}
}
//执行任务
task.resume()
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
}
将在以下行中引发错误:
//获取每个索引处的数据
让teamId:Int=teams[i][“id”]as!智力!
让teamName:String=teams[i][“name”]as!一串
让teamMember:Int=teams[i][“member”]作为!智力
任何帮助都将不胜感激。替换
teams[i][...]
与:
谢谢成功了!
(teams[i] as! NSDictionary)[...]