I get back two dictionaries, I need to parse through them and put in an array two objects. I think I did a very poor job and I have a feeling there is a better way to make the code more compact and generic.
I think there is definitely a way to combine these two loops. Please advise on any better way to accomplish this:
var cases = [[String: AnyObject]]()
var statuses = [[String: AnyObject]]()
var retVal = [AnyObject]()
var dict = [String: AnyObject]()
if jsonReturnValue != nil {
let (hasError, errorMessage) = ErrorHandler.hasError(jsonReturnValue)
if hasError {
//TODO Display a proper error to the user
)
print("Error Message: \(errorMessage)")
} else {
for value in jsonReturnValue {
for (var i = 0; i < value.1.count; i++) {
if let dataDict = value.1 as? [String: AnyObject] {
let status = dataDict["status"]
let case = dataDict["case"]
// I think I can combine the two loops below but not sure how.
if let val = status as? [AnyObject] {
if let items = val[i] as? [String: AnyObject] {
for item in items {
dict[item.0] = item.1
}
}
cases.append(dict)
}
dict.removeAll()
if let val = case as? [AnyObject] {
if let items = val[i] as? [String: AnyObject] {
for item in items {
dict[item.0] = item.1
}
}
statuses.append(dict)
}
}
}
}
}
}
retVal.removeAll()
retVal.append(cases)
retVal.append(statuses)
return retVal
}
jsonReturnValue
and what does it (typically) contain? Where doesErrorHandler.hasError()
come from? \$\endgroup\$ – Martin R Nov 19 '15 at 5:52jsonReturnValue
,ErrorHandler
, the enclosing function and use something other thancase
for your variable sincecase
is a keyword and can't be used. \$\endgroup\$ – Kametrixom Nov 21 '15 at 14:34