2015-05-24 37 views
5

Ho un oggetto swiftyJSON che assomiglia ad esempio:Come combinare due oggetti SwiftyJSON

[{ 
    "location" : "http://...", 
    "img" : "http://...", 
    "commentCount" : 0, 
    "timestamp" : 1432460217550, 
}] 

io voglio essere in grado di aggiungere un altro oggetto swiftyJSON ad esso in modo che sembra:

[{ 
    "location" : "http://...", 
    "img" : "http://...", 
    "commentCount" : 0, 
    "timestamp" : 1432460217550, 
}, 
{ 
    "location" : "http://...", 
    "img" : "http://...", 
    "commentCount" : 1, 
    "timestamp" : 1432460217571, 
} 
] 

Non riesco a utilizzare += o .append su oggetti swiftyJSON. Come posso fare questo?

risposta

0

La risposta di Victor non ha funzionato per me. Ma ho risolto il problema mettendo il mio oggetto JSON, data, in un array come questo:

var data: [JSON] = [] 

e utilizzando il seguente codice:

self.data = self.data + JSON["content"].arrayValue 
8

Come hai detto, swiftyJSON non ha una funzionalità di aggiunta.

Ciò che si può fare è analizzare gli oggetti swiftyJSON in una matrice di tipo anyObject e aggiungerli.

let json = JSON(data: data!) 
var JSONObject = JSON(json["content"].arrayObject! + json["content"].arrayObject!) 

dati -> NSData oggetto ricevuto da una richiesta HTTP.

4
extension JSON { 
    mutating func merge(other: JSON) { 
     for (key, subJson) in other { 
      self[key] = subJson 
     } 
    } 

    func merged(other: JSON) -> JSON { 
     var merged = self 
     merged.merge(other: other) 
     return merged 
    } 
} 
2

Mi è piaciuta la risposta di @ user2215977, ma avevo anche bisogno di unire i JSON nidificati. Ho esteso l'estensione per unire JSON e array nidificati, mentre gli array che contengono JSON non sono uniti, ma sono entrambi nella matrice del JSON appena generato.

importazione SwiftyJSON

extension JSON { 
    mutating func merge(other: JSON) { 
     if self.type == other.type { 
      switch self.type { 
       case .dictionary: 
        for (key, _) in other { 
         self[key].merge(other: other[key]) 
        } 
       case .array: 
        self = JSON(self.arrayValue + other.arrayValue) 
       default: 
        self = other 
      } 
     } else { 
      self = other 
     } 
    } 

    func merged(other: JSON) -> JSON { 
     var merged = self 
     merged.merge(other: other) 
     return merged 
    } 
} 

Al fine di illustrare l'utilizzo vi posterò le mie prove per questa estensione pure.

import XCTest 
import SwiftyJSON 

class JSONTests: XCTestCase { 
    func testPrimitiveType() { 
     let A = JSON("a") 
     let B = JSON("b") 
     XCTAssertEqual(A.merged(other: B), B) 
    } 

    func testMergeEqual() { 
     let json = JSON(["a": "A"]) 
     XCTAssertEqual(json.merged(other: json), json) 
    } 

    func testMergeUnequalValues() { 
     let A = JSON(["a": "A"]) 
     let B = JSON(["a": "B"]) 
     XCTAssertEqual(A.merged(other: B), B) 
    } 

    func testMergeUnequalKeysAndValues() { 
     let A = JSON(["a": "A"]) 
     let B = JSON(["b": "B"]) 
     XCTAssertEqual(A.merged(other: B), JSON(["a": "A", "b": "B"])) 
    } 

    func testMergeFilledAndEmpty() { 
     let A = JSON(["a": "A"]) 
     let B = JSON([:]) 
     XCTAssertEqual(A.merged(other: B), A) 
    } 

    func testMergeEmptyAndFilled() { 
     let A = JSON([:]) 
     let B = JSON(["a": "A"]) 
     XCTAssertEqual(A.merged(other: B), B) 
    } 

    func testMergeArray() { 
     let A = JSON(["a"]) 
     let B = JSON(["b"]) 
     XCTAssertEqual(A.merged(other: B), JSON(["a", "b"])) 
    } 

    func testMergeNestedJSONs() { 
     let A = JSON([ 
      "nested": [ 
       "A": "a" 
      ] 
     ]) 

     let B = JSON([ 
      "nested": [ 
       "A": "b" 
      ] 
     ]) 

     XCTAssertEqual(A.merged(other: B), B) 
    } 
}