Pull a specific key from an array objective c - 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.

Related

How to store a set of strings in Redshift?

I need to store an array of strings as a table column in Redshift and then check whether this array contains some string.
Since Redshift doesn't support array types, I started looking for ways around it. The only thing I came up is to encode this array as a pipe-separated string, previously escaping all the pipes within the elements of the array. Looking up of the element will be done using regexps.
While this solution seems to be viable, it requires some pre- and post-processing. Maybe you can recommend some alternatives?

Implement insertion-ordered dictionary in 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]}

Use Postgres to parse stringified JSON object

I've been using Postgres to store JSON objects as strings, and now I want to utilize PG's built-in json and jsonb types to store the objects more efficiently.
Basically, I want to parse the stringified JSON and put it in a json column from within PG, without having to resort to reading all the values into Python and parsing them there.
Ideally, my migration should look like this:
UPDATE table_name SET json_column=parse_json(string_column);
I looked at Postgres's JSON functions, and there doesn't seem to be a method of doing this, even though it seems pretty trivial. For the record, my JSON objects are just one-dimensional arrays of strings.
Is there any way to do this?
There is no need for a parse_json column, just change the type of the column:
ALTER TABLE table_name
ALTER COLUMN json_column TYPE json USING json_column::json;
Note that if you plan on doing a lot of JSON operations on these values (i.e. extracting elements from objects, modifying objects etc) it's better to use jsonb. json should only be used for storing JSON data. Also, as Laurenz Albe points out, if you don't need to do any JSON operations on these values and you are not interested in the validation that postgresql can do on them (e.g. because you trust that the source always provides valid JSON), then using text is a perfectly valid option (or bytea).

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.