I'm trying to loop through an embedded JSON array and extract all the values to put in a local array. This is what the JSON looks like:
"welcome": {
"data": {
"tncUrl": ""
},
"items": [
{
"newUser": [
{
"stepConcept": false
},
{
"stepSafety": true
},
{
"stepFacilitator": true
},
{
"stepTransparency": true
}
],
"switcher": [
{
"stepConcept": true
},
{
"stepSafety": true
},
{
"stepFacilitator": true
},
{
"stepTransparency": true
}
]
}
]
}
I'm able to get to a point where I can see I'm retrieving values for "newUser", the problem is looping through those values and adding them to an array. I'm getting a EXC_BAD_INSTRUCTION error when doing so. This is the code I'm using to get those values:
func prepareArrayOfViews(userType: User)
{
if (welcomeJSON != nil)
{
let items : NSArray? = welcomeJSON!.value(forKey: "items") as? NSArray
if (items == nil)
{
listOfViews = ["stepConcept", "stepSafety", "stepFacilitator", "stepTransparency"]
maxPages = listOfViews.count
return
}
if (items != nil) {
if let newUser = (items?.value(forKey: "newUser") as? NSArray){
//Below is where the error "EXC_BAD_INSTRUCTION"
for key in (newUser as! NSDictionary).allKeys
{
if (((newUser as! NSDictionary).value(forKey: key as! String) as? Bool)!)
{
listOfViews.append(key as! String)
}
}
}
if (listOfViews.count == 0)
{
listOfViews = ["stepConcept", "stepSafety", "stepFacilitator", "stepTransparency"]
}
maxPages = listOfViews.count
}
}
}