我的代码是我对函数的调用之前执行的。 我的功能是返回我触摸的页面内容。
这是我的功能
func Post(data: String, url: String) -> Array<Dictionary<String, String>>{
var resultArray = Array<Dictionary<String,String>>()
let session = NSURLSession.sharedSession()
let request = NSMutableURLRequest(URL: NSURL(string: "http://genysix.alwaysdata.net/"+url+".php")!)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.HTTPMethod = "POST"
request.HTTPBody = data.dataUsingEncoding(NSASCIIStringEncoding)
let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
if let _ = error {
}
if let response = response {
//Tester si la réponse est bonne par rapport au code d'erreur http
let httpResponse = response as! NSHTTPURLResponse
if (httpResponse.statusCode >= 200 && httpResponse.statusCode < 300){
do{ //Si la réponse est du JSON
let resultJSON = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments)
resultArray = resultJSON as! Array<Dictionary<String, String>>
print(resultArray)
}catch _{
}
//Si le code de la réponse n'est pas entre 200 et 300 alors il y a une erreur
}else{
}//fin test erreur HTTP
}//fin test reponse
})
task.resume()
return resultArray
}
我怎么称呼:
var COUCOU = Array<Dictionary<String, String>>()
override func viewDidLoad() {
super.viewDidLoad()
let id = "5"
let strID = "\"id\":\"\(id)\""
let data = "parameters=[{"+strID+"}]"
let url = "TicketsUserShow"
COUCOU = Post(data, url: url)
print(COUCOU)
/*for table in resultArray{
for(key,contenu) in table{
print("\(key): \(contenu)")
}
}*/
}
我的结果是: 对于COUCOU:[]
for resultArray:
[["ID": "2", "Titre": "La panne de Victor", "Urgence": "Tres haute", "Etat": "Rejete"], ["ID": "3", "Titre": "La panne acceptée d\'un petit homme", "Urgence": "Tres haute", "Etat": "En attente"]]
有人有解决方案吗?