Implement insertion-ordered dictionary in objective-c - objective-c

I want to create a dictionary, similar to NSMutableDictionary, but have it keep the insertion order of the elements. What is the most efficient way to do this?

You can try keeping an NSArray for the dictionary's keys. Each time you insert entry to you dictionary, you will also insert the key to the array. The array will preserve the insertion order, and if you'll want to get the value of the first inserted entry, you can just pull it by asking the value for the first key in the array.

As you know for NSDictionary there is no rules to keep their insertion order. If I make this, I would keep an order with a value in NSArray as a wrapper value.
NSMutableDictionary *mutableDic = #{#"key" : #[val, order]}

Related

tensorflow work function with data

I have a question. For example, I have an array and want to change their boundary values. I want to write a function that changes these values. I get an array to this function.
The question is does these function link to these array or copy it?
Best regards,
Elijah.
Values of nodes are immutable. Only variables can be modified.
If you have an operation that changes some of the values it will produce a different array.
It's not specified if it's modified in place or first copied and then modified. That is implementation dependent.

Creating a table with an array field

Can someone explain to me how arrays work in SQLite?
I've tried: CREATE TABLE foo (bar ARRAY);
Works without errors but: INSERT INTO foo VALUES ([1,2]);
doesn't.
Note: The array can have any number of elements, element number is not limited.
SQLite3 does not support arrays directly. Here are all types which are supported in SQLLite -> http://www.sqlite.org/datatype3.html.
To accomplish what you need, you have to use a custom encoding (JSON for example), or use an FK, i.e. create another table, where each item in the array is stored as a row.

Comparing an NSArray of NSStrings with another NSArray of NSStrings

I have two NSArrays each containing NSStrings. I need to test whether the two arrays are equal. In this instance, equality means not that the arrays contain the same objects, but that each object returns true for isEqualToString when compared its counterpart. The arrays are also not equal if one contains more items than the other, or the order of the items is different.
Can I assume isEqualToArray won't help me here?
Similarly, I don't see an approach using NSSet that would fulfill all of the criteria.
How might I test the equality of these two arrays?
The docs for isEqualToArray state:
Two arrays have equal contents if they each hold the same number of objects and objects at a given index in each array satisfy the isEqual: test.
That seems like it fits your criteria.

Objective-C: Add JSON response to SQLite DB

I am getting a JSON response and would like to add it to an SQLite db. The results come back as an array and in each array there will be about 30 keys with values. What would be the most efficient approach to adding all those to my table? The keys coming from the JSON would be the columns in the sqlite db.
Would it be best to do a for loop on each array item then another for loop in side to get the values and add it to a string then add them to the database that way? Or is there a better approach using FMDB to add a JSON response directly to the database if the json keys match the database table columns?
If you believe your JSON response will not change and your data model will not change (or rarely change), then I'd just loop through the arrays and write the slightly long...
[db executeUpdate:#"INSERT INTO response (key1,key2,..key30) VALUES (?,?,?...etc);", json_reponse.value1,json_response.value2,...,json_response.value30, nil];
However, if this model would change, be extended, etc... then I'd probably just use Core Data.
The biggest factor though is what are you doing with the data after it is stored? Creating objects, display a report, converting the objects back to JSON (then just store the raw JSON in a text field)?
I ended up doing a loop and getting the json keys and using those as the columns.

Pull a specific key from an array objective c

in PHP when using an array you can use myArray['firstElement'] to pull that specific key and its associated values from the array. How do I do this similar type of thing in objective c?
If you want to deal with key-value pairs, you would use an NSDictionary.
Although it is surprising to note that arrays in PHP are an ordered map.
You want to use NSDictionary instead of NSArray. NSDictionary is an associative array.