Problem with object in Nsarray

1

What I want to do is just grab the idCategoria of the object to be able to use it in another query but it marks the following error:

  

can not convert value of string to expected argument to int

let objeto = values["idCategoria"]
print(objeto)'

Complete code

import Foundation
import UIKit
import WebKit

class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet var tableView: UITableView!
//var identificador = ["1","1","1","1","1","1","1","1","1"]

var values:NSArray = []


//var arr =  ["http://totalplanning.guiaparatuseventos.com/IOs/categorias.php"]

//let colors = [UIColor.brownColor(),UIColor.purpleColor(), UIColor.yellowColor(), UIColor.greenColor(), UIColor.blueColor(), UIColor.grayColor()]

override func viewDidLoad() {
    super.viewDidLoad()

    let proxyViewForStatusBar : UIView = UIView(frame: CGRectMake(0, 0,self.view.frame.size.width, 20))
    proxyViewForStatusBar.backgroundColor = UIColor(red: 0.48627450980392156, green: 0.070588235294117646, blue: 0.46274509803921571, alpha: 1)
    self.view.addSubview(proxyViewForStatusBar)


    get();
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func hexStringToUIColor (hex:String) -> UIColor {
    var cString:String = hex.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet() as NSCharacterSet).uppercaseString

    if (cString.hasPrefix("#")) {
        cString = cString.substringFromIndex(cString.startIndex.advancedBy(1))
    }

    if ((cString.characters.count) != 6) {
        return UIColor.grayColor()
    }

    var rgbValue:UInt32 = 0
    NSScanner(string: cString).scanHexInt(&rgbValue)

    return UIColor(
        red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
        green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
        blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
        alpha: CGFloat(1.0)
    )
}


func get(){
    let url = NSURL(string:"http://totalplanning.guiaparatuseventos.com/IOs/categorias.php")
    let data = NSData(contentsOfURL: url!)

    values = ((try! NSJSONSerialization.JSONObjectWithData(data!, options: [])) as? NSArray)!

}




func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {



    return values.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{



    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! SpecialCell
    let maindata = values[indexPath.row]
    //NSLog("La liga es %@ %@", maindata["idCategoria"] as! String, maindata["nombre"] as! String);
    cell.nombre.text = maindata["nombre"] as? String
    cell.descripcion.text = maindata["descripcion"] as? String
    //cell.idCategoria.text = maindata["idCategoria"] as? String

   let colors = [hexStringToUIColor("#FF0000"),hexStringToUIColor("#FF8000"),hexStringToUIColor("#FFFF00"),hexStringToUIColor("#80FF00"),hexStringToUIColor("00FFFF"),hexStringToUIColor("#0080FF"),hexStringToUIColor("#A901DB"),hexStringToUIColor("#FF00FF"),hexStringToUIColor("#FF0080")]

    cell.accessoryType = .DisclosureIndicator
    cell.imagen.backgroundColor = colors[indexPath.row % colors.count]





    return cell

}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {



    let objeto = values["idCategoria"]
    print(objeto)

}

}
    
asked by Brandon Josué Cerezo 03.08.2016 в 20:02
source

1 answer

0

values is an array of JSON and therefore you must first take the object and then access its property, that is:

let objeto = values[indexPath.row]["idCategoria"]
print(objeto)
    
answered by 03.08.2016 / 23:21
source