You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift中使用Theo执行Neo4j事务时如何读取返回数据?

Hey there! Let's break down your question step by step:

First, what type is the response object?

From the printed output and how Theo's executeTransaction works, the response is a [String: Any] dictionary (Swift's type-erased dictionary). It bridges from Objective-C collections like NSDictionary and NSArray under the hood, so we can safely cast its values to Swift's native collection types when parsing.

How to extract your node properties and relationships

Since you included both "row" and "graph" in resultDataContents, the response gives you two ways to access the data—let's cover both approaches, plus how to grab relationships if they exist later.

Step 1: Unwrap the top-level response safely

First, we need to safely extract the results array and check for errors to avoid crashes:

theo.executeTransaction(statements, completionBlock: {(response, error) in
    // Handle immediate transaction errors first
    if let error = error {
        print("Transaction failed: \(error.localizedDescription)")
        return
    }
    
    // Safely cast the response to a Swift dictionary
    guard let responseDict = response as? [String: Any],
          let results = responseDict["results"] as? [[String: Any]],
          let errors = responseDict["errors"] as? [[String: Any]], errors.isEmpty else {
        print("Invalid response structure or errors present")
        return
    }
    
    // Now parse the results...
})

Step 2: Extract simplified properties from the row field

The row field gives you a stripped-down view of the node properties directly. Since your query returns n, each row entry is an array containing one element: the node's properties dictionary.

for result in results {
    guard let dataItems = result["data"] as? [[String: Any]] else { continue }
    
    for item in dataItems {
        // Extract the row array (contains our node's properties)
        if let row = item["row"] as? [[String: Any]],
           let nodeProps = row.first { // Our query returns one node per row, so take the first element
            
            // Pull out specific properties you need
            if let imageUrl = nodeProps["image"] as? String {
                print("Found image URL: \(imageUrl)")
                // Use this URL to load images in your app
            }
            
            if let note = nodeProps["note"] as? String {
                print("Found note: \(note)")
                // Display the note text in your UI
            }
        }
    }
}

Step 3: Extract full node/relationship data from the graph field

If you need more details (like node ID, labels, or relationships), use the graph field. This gives you a complete graph structure of the returned data:

for result in results {
    guard let dataItems = result["data"] as? [[String: Any]] else { continue }
    
    for item in dataItems {
        guard let graph = item["graph"] as? [String: Any] else { continue }
        
        // Extract full node details
        if let nodes = graph["nodes"] as? [[String: Any]] {
            for node in nodes {
                let nodeId = node["id"] as? Int
                let labels = node["labels"] as? [String]
                let properties = node["properties"] as? [String: Any]
                
                print("Node ID: \(nodeId ?? -1)")
                print("Labels: \(labels ?? [])")
                
                // Extract properties from the node object
                if let image = properties?["image"] as? String {
                    print("Image (full graph): \(image)")
                }
                if let note = properties?["note"] as? String {
                    print("Note (full graph): \(note)")
                }
            }
        }
        
        // Extract relationships (if your query returns any in the future)
        if let relationships = graph["relationships"] as? [[String: Any]] {
            for rel in relationships {
                let relId = rel["id"] as? Int
                let relType = rel["type"] as? String
                let startNodeId = rel["start"] as? Int
                let endNodeId = rel["end"] as? Int
                let relProperties = rel["properties"] as? [String: Any]
                
                print("Relationship ID: \(relId ?? -1)")
                print("Type: \(relType ?? "")")
                print("Connects node \(startNodeId ?? -1) to \(endNodeId ?? -1)")
                
                // Handle relationship properties if needed
                if let relationshipProp = relProperties?["your_prop_key"] as? String {
                    print("Relationship property: \(relationshipProp)")
                }
            }
        }
    }
}

Quick recap of the response structure

To make it easier to navigate, here's the hierarchy of the response:

  • Top-level [String: Any] with two keys:
    • "results": Array of query result objects ([[String: Any]])
    • "errors": Array of error objects (empty if the transaction succeeded)
  • Each result object contains a "data" array, where each entry is a single returned record
  • Each record has:
    • "row": Simplified array of returned values (your node's properties)
    • "graph": Full graph structure with "nodes" and "relationships" arrays
    • "meta": Metadata about the record (like whether the node was deleted)

内容的提问来源于stack exchange,提问作者bigreddawg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:47:35