In my ios app I'm allowing the user to create a group. Whenever they create a group the group JSON values from my php file is returned and stored in a dictionary called groups:
var groups : NSDictionary?How can I access a value from this dictionary? For example if a group has an Id of 1, how can I pull all of the data from group 1? Sorry I'm very new to swift.
Your json return data with key : value : you should see in php script for instance what are key and value.
If keys are STrings as Key1, Key2, … and values are Strings
You should have code like this (of course need to adapt to your real json data definition):
var groups : NSDictionary? /
do {
let aDictionary : NSDictionary? = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary
if let actualDictionary = aDictionary {
groups = actualDictionary
}
else {
// aDictionary is nil
}
}
catch {
print("Error ", error)
}
if let parseJSON = groups {
// Okay, the parsedJSON is here, let's get the value for 'success' out of it
if let value1 = parseJSON["key1"] as? String { // You have recovered the data from group 1