2
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 per hour fill the tableView with only the value nameServico. However, when I print I see that the function consultService failed to popular the variable values:
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?
Have you checked what comes in
response
?– Jeferson Assis
Try instead of placing on a Let, calling the values[indexPath.Row] straight into the cell string
– JdsMedeirosBR
I’ve tried that, but the problem is that I’m not actually able to fill in the "values" variable with the return of json. I think it would be more accurate to say that I’m not getting the callback.
– Fábio Jânio
The json is being returned, I already tested it and the return is what I passed in the first block of code. But I’m not able to play this return inside the variable "values".
– Fábio Jânio
When you pass
self.valores = jsonResult
, is passing an immutable object to a mutable object, try to create a new object by taking as a basis thejsonResult
using the method initWithDictionary– Jeferson Assis
I’ve tried a lot of alternatives, but nothing works. From what I could see I can’t get the returned json inside the clousure "closure" and cry into an external variable the same.
– Fábio Jânio
Can you post this url somewhere so I can simulate?
– Jeferson Assis
I published a similar json I’m using at: http://fabiojanio.com/json/json.php
– Fábio Jânio
Fábio, here we do not mark the question as solved in the title, just accept an answer as you already did.
– bfavaretto