Swift - JSON anyobject转换为String失败的tableview

时间:2016-03-27 01:29:05

标签: ios json swift neo4j

我正在使用JSON数组字典:

var objects: Array<Dictionary<String, AnyObject>> = Array<Dictionary<String, AnyObject>>()

这是使用Neo(Neo4j的开源swift接口)从Theo4j服务器返回的

我正在努力将数据转换为String,以便将数据检索到TableView中。

以下是我的JSON查询返回数据的示例:

objects = [["id": <null>, "born": 1964, "name": Keanu Reeves], ["id": <null>, "born": 1967, "name": Carrie-Anne Moss], ["id": <null>, "born": 1961, "name": Laurence Fishburne], ["id": <null>, "born": 1960, "name": Hugo Weaving], ["id": <null>, "born": 1967, "name": Andy Wachowski], ["id": <null>, "born": 1965, "name": Lana Wachowski], ["id": <null>, "born": 1952, "name": Joel Silver], ["id": <null>, "born": <null>, "name": Ann], ["id": <null>, "born": <null>, "name": Dan]]

我正在尝试将名称和生成数据(如果存在)拉入tableview。由于字典的AnyObject字段没有引号,是否会导致转换问题?这是我的代码:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell

    executeCypherQuery()
    let object = objects[indexPath.row]
    print("Object is: \(objects)")
    print("Hello")
    cell.textLabel!.text = object["name"] as! String
    cell.detailTextLabel!.text = object["born"] as! String
    self.tableView.reloadData()
    return cell
}

我收到一条错误消息,说它无法将数据转换为String(我也尝试转换为NSString)。

我尝试在操场上运行一些代码并注意到它被列出的AnyObject数据(如名称)混淆了,因为它在这里显示。由于名称可能有两个数据字,因此需要“,”来分隔它们,但这不是正确的格式。

感谢任何见解。

谢谢!

编辑:这是来自Neo4j的开源项目Theo的JSON解析代码:

    public func executeCypher(query: String, params: Dictionary<String,AnyObject>?, completionBlock: Client.TheoCypherQueryCompletionBlock?) -> Void {

    // TODO: need to move this over to use transation http://docs.neo4j.org/chunked/stable/rest-api-cypher.html

    var cypherPayload: Dictionary<String, AnyObject> = ["query" : query]

    if let unwrappedParams: Dictionary<String, AnyObject> = params {
        cypherPayload["params"] = unwrappedParams
    }

    let cypherResource: String = self.baseURL + "/db/data/cypher"
    let cypherURL: NSURL = NSURL(string: cypherResource)!
    let cypherRequest: Request = Request(url: cypherURL, credentials: self.credentials)

    cypherRequest.postResource(cypherPayload, forUpdate: false, successBlock: {(data, response) in

        if (completionBlock != nil) {

            if let responseData: NSData = data {

                dispatch_async(self.parsingQueue, {

                    let JSON: AnyObject! = try? NSJSONSerialization.JSONObjectWithData(responseData, options: NSJSONReadingOptions.AllowFragments)

                    let jsonAsDictionary: [String:[AnyObject]]! = JSON as! [String:[AnyObject]]
                    let cypher: Cypher = Cypher(metaData: jsonAsDictionary)

                    dispatch_async(dispatch_get_main_queue(), {
                        completionBlock!(cypher: cypher, error: nil)
                    })
                })

            } else {
                //TRAVIS EDIT: UNNECESSARY?
                //completionBlock!(cypher: nil, error: self.unknownEmptyResponseBodyError(response))
            }
        }

        }, errorBlock: {(error, response) in

            if (completionBlock != nil) {
                completionBlock!(cypher: nil, error: error)
            }
    })
}

0 个答案:

没有答案