Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It seems not easy to manipulate an array or dictionary? #110

Closed
phu4ku opened this issue Dec 10, 2014 · 5 comments
Closed

It seems not easy to manipulate an array or dictionary? #110

phu4ku opened this issue Dec 10, 2014 · 5 comments

Comments

@phu4ku
Copy link

phu4ku commented Dec 10, 2014

for example, delete one item, append one item. the setter can change the whole array or dictionary. but how can i just manipulate an array or dictionary?

@ArtSabintsev
Copy link

Having the exact same issue.

@ArtSabintsev
Copy link

Figured it out! On your returned JSON object, just call the optional vars .array or .dictionary.

If your object is an array and you rightfully call .array, then you can use all the Array methods, like append. If it happens to not be an array, nil is returned, as it is an optional var.

Array Example

let json = JSON(data: data)
let array = json.array // Returns an [AnyObject]?, which means you'll get an [AnyObject] or nil

Dictionary Example

let json = JSON(data: data)
let dictionary = json.dictionary // Returns an [String: JSON]?, which means you'll get an [String: JSON] or nil

@artworkad
Copy link

@ArtSabintsev but array is immutable, you cannot append to it

@ArtSabintsev
Copy link

Then you can make a new immutable array and populate it with the data in the immutable array.

@alexhunsley
Copy link

alexhunsley commented Mar 8, 2018

I'm having the same problem. I just cannot get the JSON to update.

let wholeJSON: JSON = .... // my json tree
let dictObject: JSON = ... // some object from inside wholeJSON

 guard var myDict = dictObject.dictionary else {
     fatalError("Didn't find dict")
 }

 // modify the dict
myDict["a"] = "b"

// is this necessary? But even with this line below, still don't
// get the json updated
dictObject.dictionary = myDict

// the call below doesn't show any of my changes in the data
wholeJSON.rawData()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants