ios - How to access local variable in for loop through dictionary or dictionaries when only one dictionary is available? -


I have a loop to go through the dictionaries of dictionaries. It only works fine unless only one dictionary Not total

Here is the summary of my method:

  - (NSArray *) objects FROMJSON: (NSString *) jsonString error: (NSError **) error {NSParameterAssert (jsonString! = Zero); // Create JSON object NSData * jsonData = [jsonString dataUsingEncoding: NSUTF8StringEncoding]; NSError * localError = Nil; Id jsonObject = [NSJSNSRialization JSONObjectWithData: jsonData options: 0 error: & amp; Local error]; NSDictionary * dictionaryOfDictionaries = (id) jsonObject; // If not successfully parsed, then error (dictionaryoffdexin == zero) {if (error! = NULL) {* Error = [NSER Error: Dird Error: Error Domain Code: Locked DataAir User Inc: Zero]; } Return zero; } / / Create objects from other parsed data NSMutableArray * objectsArray = [NSMutableArray array]; (Objectivity [@ "road"] City: Objective [@ "city"] For the state: Object [["" state "]; // If error, return if (object == blue) {if (error!) { * Error = [NSER Error: With Dashman: Error Domain Code: Missing DataArchor User Information: Zero];} Return Zero;} // Add more successfully created object array [Objects Ore Object: Object];} Return [Objective copy] ;}  

As told by It works fine - that is, object s has been successfully created from objectDictionary - if there is more in one dictionary, the dictionary dictionary However, if there is only one dictionary, then being typed for objectDictionary in the id for the loop (rather than the obj ) , And use its values, such as objectDictionary [@ "name"] result in an error.

How do I write it in the most efficient way , So that a dictionary is handled?

Here's the problem when you have more than one dictionary In fact, in each word, jsonObject actually contains each dictionary from a NSArray .

When there is only one dictionary, jsonObject is only one dictionary.

Replace your code like this:

  id jsonObject = [NSJSNASialization JSONObjectWithData: jsonData option: 0 error: and local error]; NSArray * arrayOfDictionaries = Zero; If (jsonObject == zero) {// handle error} and if ([JasonOnbys iskindoff class: [NSARRA class]]) {arrayOfDictionaries = jsonObject; } And {arrayOfDictionaries = @ [json object]; }   

for loop:

  (NSDictionary in objectDictionary in arrayDictionary)  

Comments

Popular posts from this blog

c# - SignalR: "Protocol error: Unknown transport." when navigating to hub -

class - Kivy: how to instantiate a dynamic classes in python -

python - mayavi mapping a discrete colorbar on a surface -