I'm trying to Parse JSON with code and structure like this:
userApiService.getAllUsers { (responseDict:NSDictionary?, error:NSError?) -> Void in
//Parse responseDict for the key "result"
}
Here is Json Structure
{
error = "";
result = (
{
name = AnotherUser;
password = AnotherPassword;
userId = 1343;
},
{
name = TestUser;
password = TestPassword;
userId = 1344;
},
{
name = TestUser;
password = TestPassword;
userId = 1347;
},
);
status = 200;
}
I've tried code like this:
self.loadingIcon.endRefreshing()
if let resultDict = responseDict["result"] as? NSArray {
for userRecord in resultDict{
var userModel = User(userDict: userRecord as! NSDictionary)
self.tableData.append(userModel)
}
}
self.tblView.reloadData()
}
But this results in the error "NSArray?" is not convertible to StringLiteralConvertible If I remove the optional and add the ! force unwrap to the closure signature then this error goes away. However, I've seen instances where my app was crashing if something wrong came from the backend. So my questions are:
Is there a way to parse this JSON and still keep the optional NSDictionary in the closure signature.
Or do I just need to check if the dictionary is not nil and then proceed with the code I posted above?