Search code examples
iosarraysjsonswiftswifty-json

How to append new data to an existing JSON array(swiftyJSON)


I have an array of SwiftyJson data that I have declared and filled it with data .The code I'm using to fill the hoge array is this : self.hoge = JSON(data: data!)

but I need to append new swiftyJSON data to this hoge array .I noticed hoge array doesn't have append property . How should I do that?

Thanks


Solution

  • SwiftyJSON does not have append or extend functionality.

    You can:

    self.hoge = JSON(self.hoge.arrayObject! + JSON(data: newData).arrayObject!)
    

    But I recommend to declare self.hoge as [JSON]

    var hoge:[JSON] = []
    
    func readMoreData() {
    
        let newData: NSData = ...
    
        if let newArray = JSON(data:newData).array {
            self.hoge += newArray
        }
    }