I have a web page that returns the following json structure:
[
{
"nomeDoServico":"fdPHost",
"nomeParaExibicao":"Host de Provedor da Descoberta de Função",
"status":"Iniciado"
},
{
"nomeDoServico":"LanmanWorkstation",
"nomeParaExibicao":"Estação de trabalho",
"status":"Iniciado"
}
]
The code below written in Swift 2 should take this return and by time fill the tableView with only the value serviceName. However, when I print I see that the QueryService function was not able to populate the Values variable:
import UIKit
class ViewController: UIViewController, UITableViewDelegate {
var valores = [Dictionary<String, String>]()
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return valores.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
let valor = valores[indexPath.row]
cell.textLabel?.text = valor["nomeDoServico"]
return cell
}
func consultarServico() -> Void
{
let myUrl = NSURL(string: "http://192.168.56.101/database_servico.php")
let request = NSMutableURLRequest(URL:myUrl!)
let postString:String = "id_menu=1"
request.HTTPMethod = "POST"
request.timeoutInterval = 5.0
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) in
if (error?.code == -1001)
{
print("Opa, timeout")
}
do {
let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! [Dictionary<String, String>]
self.valores = jsonResult
}
catch
{
print("Erro ao buscar os dados")
}
}
task.resume()
}
override func viewDidLoad() {
super.viewDidLoad()
// ocultar teclado ao clicar na view
let tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "DismissKeyboard")
self.view.addGestureRecognizer(tap)
consultarServico()
}
// encerra o teclado
func DismissKeyboard(){
self.view.endEditing(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Could someone please help me?