I have to show the response with same order as I have received from my backend team.
But the response sequence is getting changed on extracting the values from Dictionary and array.
Please check the attached screenshot and suggest .
On extracting , I am getting 7703 but I need 5599 for showing the same position as in my website and in my application
Dictionary is not an ordered collection. You should create custom model class and use array of models.
You can use famous third party https://github.com/jsonmodel/jsonmodel
for mapping json to array of models.
Or you can sort dictionary keys (dict.allKeys) or values (dict.allValues)
Related
I am trying to set values in a nested dictionary - similar to pythonic dict["key1"]["key2"]=value1 in Apple's my shortcut app on IOS 13. I fail even with a simple dict see attached screenshot which should add an element to the dict. The endgame is to modify EXIF metadata via the Metapho app - non-working example attached
I used ttwo approached
use a basic operation to modify dictionary
use prexisting shortcuts to modify dicitonaries as outline on the following Reddit threat Dictionary utilites.
To reproduce you need to download the attached shortcuts - and Dictionary utilties for the examples using those.
I first checked if I can modify a non-nested dict and nonnested dict.
Modifying non-nested dictionary
Using core tools I cannot add a key-value pair to a dictionary.
Minmal non-working exampleas per below.
Modifying non-nested dictionary using Dictionary tools
Minmal working example
Modifying nested dictionary using Dictionary tools
Using the same tools to modify nested dicts does not work.
Minimal non working example
Any help greatly appreciated. Asked same question on ask different
If values of a dictionary are modified the variable needs to be reset:
reset variable
Using this both nested and non-nested dicts can be modified without external tools.
Essentially this is called by value instead of call by reference. If those tools are used still their output needs to overwrite the variable in question.
Regarding modifying the underlying Exif values it is important it is important that again the output imagevariable needs to be written to new file in the photo app.
Mac
I think this is a probably repeat question.
I am new to ios and and I am trying to learn so I don't expect response in actual codes. I still want to write them myself.
Here's what I have done so far:
I wrote codes to make web api calls, authenticate and pull json data.
I've figured out how to parse json data that I pulled from web.
I have made tableview controller.
Challenge: How do I get the parsed Json data to tableview controller?
Do I:
- save it locally into like NSUSERDefaults variable?
- save it to a global variable shared by controller and my model?
- at the end of the parsing json data do a return so that it will be saved into viewController?
- or none of the above and do ______________ (insert your answer)
What's the cleanest way to do this? The table will initially have like 20 rows but as you pull down you will get more rows.
Best way to save data on model class while parsing and store this class objects in an array and in cellforrowatindexpath get objects of model class from array and show data using these objects.
I'm using Parse as the backend for my app. My app will be used in the field where service will nonexistent or spotty at best so I need to store information offline. I currently save data for the user in a plist in the background (Title, location coordinates, notes, additional data). Since Parse's current iOS offline saving is fairly poor (From what I've read), I was hoping to get around it by creating an array or dictionary from the plist and upload that to Parse by giving it an array once the user is back in cell range.
As it occurs now, when I upload the array, it simply puts the entire contents of the array in a single cell in the database. Is there a way to parse the array and create a new row for each entry/object in the array?
I may have overlooked a better way to do this. If someone has a suggestion I would appreciate it!
I solved it. I iterated through the array using a for loop and added each index as a separate object.
I am using AFNetworking to retrieve JSON data from a web service. Part of the response string I get is:
{"DATA":{"LEASE TYPE":"3 Yrs + 0 renew of 0 Yrs","LANDLORD":"","TENANT":"test comp"...
and so on. The order of the key values in the "DATA" dictionary ("LEASE TYPE","LANDLORD","TENANT"...) is important for presentation purposes. However, when AFNetworking calls NSJSONSerialization's:
+ (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error;
method, the returned dictionary has the keys in a different order.
I notice that the AFJSONRequestOperation object has the server's response stored as an NSString, with everything in the correct order. However I'm not keen on parsing the JSON by hand if I can avoid it.
Can anyone suggest a way that will let me get at / keep the keys in their original order?
Thanks.
If the order is important use an array not a dictionary, dictionaries are be by their nature unordered. Or add an array of dictionary keys in the order desired.
If you have no control over the response that is sent you will have to parse the JSON yourself at least for the ordering.
When you'r creating an NSDictionary, the order will not be the same. I often recognized that dictionaries get ordered by key-name alphabetically.
But when using dictionaries the order doesn't really matter. And they shouldn't!
As the previous answers mentions dictionaries are by nature without order, but you can find here a nice class of OrderedDictionary:
http://www.cocoawithlove.com/2008/12/ordereddictionary-subclassing-cocoa.html
http://projectswithlove.com/projects/OrderedDictionary.zip
I am reading on objective-c (a nerd ranch book), and I can't help thinking about this question: How do I decide which collection type, NSArray or NSDictionary (both with or w/o their mutable subclasses), to use when reading content from URL?
Let's say am reading JSON data from a PHP script (a scenario am dealing with), which to use? I know it is stated in many references that it depends on structure of data (i.e. JSON), but could a clear outline of the two structures be outlined?
Thank you all for helping :)
NSArray is basically just an ordered collection of objects, which can be accessed by index.
NSDictionary provides access to its objects by key(typically NSStrings, but could be any object type like hash table).
To generate an object graph from a JSON string loaded via a URL, you use NSJSONSerialization, which generates an Objective-C object structure. The resulting object depends on the JSON string. If the top-level element in your JSON is an array (starts with "["), you'll get an NSArray. If the top-level element is a JSON object (starts with "{"), you'll get an NSDictionary.
You want to use NSArray when ever you have a collection of the same type of objects, and NSDictionary when you have attributes on an object.
If you have, lets say a person object containing a name, a phone number and an email you would put it in a dictionary.
Doing so allows the order of the values to be random, and gives you a more reliable code.
If you want to have more then one person you can then put the person objects in an array.
Doing so allow you to iterate the user objects.
"withContentOfURL" or "withContentOfFile" requires the data in the URL or the file to be in a specific format as it is required by Cocoa. JSON is not that format. You can only use these methods if you wrote the data to the file or the URL yourself in the first place, with the same data. If you write an NSArray, you can read an NSArray. If you write an NSDictionary, you can read an NSDictionary. Everything else will fail.