从NSDictionary问题中调用特定数据键值

时间:2015-01-16 15:50:36

标签: ios swift nsarray nsdictionary

在cellForRowAtIndexPath中调用NSDictionary中的值,但是我的数据(parsedData)为nil。请问如何从NSDictionary键值获取数据?

var dic1 = NSDictionary()

func parser(parser: NSXMLParser!, didStartElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!, attributes attributeDict: [NSObject : AnyObject]!)
{
    element = elementName

    if (elementName as NSString).isEqualToString("info")
    {
        symbolStr = attributeDict["symbol"]! as NSMutableString
        offerlStr = attributeDict["offer"]! as NSMutableString

        dic1 = ["symbolName":symbolStr , "offerAmount":offerlStr]

        pariteArray.addObject(dic1)   
    }
}

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

    return pariteArray.count
}

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

    var parsedData: AnyObject? = dic1["symbolName"]?.indexPath
    var parsedData2: AnyObject? = dic1["offerAmount"]?.indexPath

    let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")

    cell.textLabel?.text = parsedData as? String //nil
    cell.detailTextLabel.text = parsedData2 as? String //nil

    return cell
}

1 个答案:

答案 0 :(得分:1)

我不确定你要做什么,但我可以告诉你为什么你的代码错了。

在此,您将dic11的值指定为NSMutableString个对象:

symbolStr = attributeDict["symbol"]! as NSMutableString
offerlStr = attributeDict["offer"]! as NSMutableString

dic1 = ["symbolName":symbolStr , "offerAmount":offerlStr]

稍后,您尝试访问这些对象的.indexPath

var parsedData: AnyObject? = dic1["symbolName"]?.indexPath
var parsedData2: AnyObject? = dic1["offerAmount"]?.indexPath

NSMutableString没有.indexPath

我会在这里猜测并假设你真的想这样做:

var parsedData: AnyObject? = pariteArray[indexPath.row]["symbolName"]
var parsedData2: AnyObject? = pariteArray[indexPath.row]["offerAmount"]