I have an array that contains 2 objects. In order to store it on my backend server service, I need to store it inside of another array.
So later on, when I call my server and tell it I want the array object, it sends me a new array object that is holding my original array object.
I need to loop through the new array (that contains my original array), and then loop through all of the objects inside of my original array.
I know how to do a normal for loop and loop through an array, but I have never had to do it like this where you need to loop through an array that is contained inside of another array.
I have been thinking about ways to do this now for about an hour and really have no clue. I think what I need to do is technically called "looping through nested arrays" but I can't seem to find anything about doing this with objective-c.
Thanks for the help.
Use a nested for loop and you can iterate through the objects in both arrays:
for(NSArray* array in arrays){
for(object* thing in array){
//do what you want with thing in arrays
}
}
Do you need to loop through every object in both arrays, or do you need to fetch the object from the outer array and just loop through that?
If you need to loop through all objects in both arrays, #JMarsh 's code will do that.
If you only need to fetch the inner array, then just use an explicit fetch Following JMarsh's format:
NSArray *innerArray = arrays[1]; //Or whatever array index is correct
for(id thing in innerArray)
{
//do what you want with thing
}
Related
I've got a value like so: #"2329300" and I've got a NSDictionary like so :{#"John Appleseed":[#"2329300",#"2342322",#"32i249"]}
How do I find the index of the key/value pair in the NSDictionary when I've only got a string value of the entire list that's known as the value. I'm assuming there's no duplicates in the dict.
I know that there's indexForObject on a NSArray but is there a similar thing for a dict?
I imagine it would look something like this:
[NSDictionary indexForValue:value]; // returns index number.
And even then the NSString doesn't match the value, so I'd need a workaround for that too.
You have a basic misunderstanding. Dictionaries are unordered collections. They do not have any particular order for their key/value pairs. You can't have indexes to the key/value pairs because that implies a fixed order.
Think of a dictionary as a bunch of kids milling around on a playground. You can call out a kid's name "Johnny, come here!" and fetch that kid (use a key to find an object) but what does order mean for kids that won't sit still?
You can create an array of the keys from a dictionary and sort that into a particular order (an alphabetical list of the kids on the playground) if that's what you want, or you can create an array of dictionaries, or an array of a custom data object that contains any arbitrary properties that you want.
EDIT:
For a table view, an array of dictionaries is probably a good choice. Each entry in the array contains a dictionary with all the settings for a cell in the dictionary. If you have a sectioned table view then you want an outer array for sections, containing inner arrays for the rows, and each entry in the inner array containing a dictionary.
I tend to prefer custom data objects to dictionaries though. (An object that just has properties for each setting I want.) That way the list of values and their types is crystal-clear and fairly self-documenting.
Here is a snippet of code that enumerates through NSMutableArray *myArray and modifies each NSMutableDictionary within it. The problem I see with this code is that it's modifying while enumerating.
Code:
for (NSMutableDictionary *aDict in myArray)
{
theDate = [self convertTheDate:[aDict valueForKey:#"date"]];
[aDict setValue:theDate forKey:#"date"];
}
[self passUpdatedDates:myArray];
How should I modify this to be safe code?
Your code should be fine (with the change to setObject:forKey: as Maddy described in his comment), as long as your array myArray really is an array of mutable dictionaries. If any of the dictionaries in the array are not mutable then this code will crash when you try to change the dictionary.
If that's the case then you will need to create a mutable copy of each dictionary, change the key in question, and then save the modified dictionary to the correct index in the array. To do THAT, you now have to deal with modifying an array as you are enumerating it.
The simple way to do this is to change from fast enumeration (for... in) to a for loop that counts through all the indexes and uses objectAtIndex to fetch each object, change it, and save it back to the array.
To continue to use fast enumeration, you can create a new mutable array that is the same size as myArray, loop through myArray with for... insyntax, and simply add each modified dictionary to the new mutable array. Then you are enumerating the source array with fast enumeration and creating a duplicate object in a separate array. This doubles your memory footprint but gives you the performance benefit of fast enumeration.
This question already has answers here:
What's the difference between a dictionary and an array?
(6 answers)
Closed 10 years ago.
I'm in a dilemma in terms of which of the two I should use. I will be retrieving a group of data via a restful API (returns json) and I'm not sure how I should store them before I display it on my UI View Table.
eg.
{"Events":[{"Id":5,"Name":"Event 1 2013"},{"Id":6,"Name":"Event 2 2013"}]}
I've been reading tutorials and some would use NSMutableArrays while some would use NSMutableDictionary.
How should I go about it?
BTW: I'm displaying the data on UI View table that will redirect the user to another page when tapped and if they decide to go back will have to show the previous view without reloading (uses UinavigationController)
Thanks!
EDIT:
Also, just to give you an idea on what I'm trying to do. I'm trying to follow this tutorial on splitting the data I get into section headers. On this tutorial it's using NSDictionary.
http://www.icodeblog.com/2010/12/10/implementing-uitableview-sections-from-an-nsarray-of-nsdictionary-objects/
If I use NSArray, would that affect the performance?
In NSArray - every item in the collection has an integer index, so there is an explicit order to the items. When you're retrieving/replacing/removing the stored object from the NSARRY,you need to specify the corresponding object index of that stored object.
NSDictionary - derived from the word called entry. Each entry consists of one object that represents the key and a second object that is that key’s value. Within a dictionary, the keys are unique. That is, no two keys in a single dictionary are equal (as determined by isEqual:).When you're retrieving the object from the dictionary you need to specify the key value for the objectForKey
Whenever if you're parsing the plist then NSDictionary would be ideal.You can refer apple's document here for more explanation about NSDictionary.Happy coding :)
The lookup times on NSDictionaries are faster than on NSArrays. That's one of the main advantages. Here's a link to the Apple documentation.
Generally, if you need to access data in an indexed fashion (like you need to for rows in a table) then you should use an array because you can access any specific index using indexOfObject:
Now, if you have a lot of information for each row then you should have an array of either custom objects or an array of dictionaries.
Dictionary are always faster than Arrays. Dictionary maps keys to objects, just like a hash table. It's an associative array.
For searching some value you need to iterate for arrays, in dictionary you retrieve it by key.
If you want the collection to be in some sorted order or arrival order then Array is the proper type for you.
Dictionary lacks when you end up getting two same keys.
And I feel good to use arrays for tableViews as I can directly associate row to index.
Now, I've looked up on this on here and google, and it seems everyone uses NSSet to remove dupes. This is cool and all, but it seems that this method removes the sorting as well.
Is there
1) A way to sort NSSet alphabetically?
2) A better way to remove dupes in NSMutableArray in or outside the for loop where I add them to the array by reading them from a .csv file.
Thanks:)
I believe you want to be using an NSOrderedSet. Here's the documentation on it:
http://developer.apple.com/library/mac/#documentation/Foundation/Reference/NSOrderedSet_Class/Reference/Reference.html
You can construct an NSOrderedSet* from your array; using +orderedSetWithArray: will preserve the array's existing order. If it's not already in the correct order, you can't sort the set directly, so there's little point in using an ordered set, but you can easily construct a regular NSSet and sort that into another array, using -sortedArrayUsingDescriptor:, or even better allObjects, followed by any of NSArray's sorting methods.
On the other hand (it's possible I'll get some nasty comments about this, but...), since NSArray and NSSet seem to be built on top of the same hash table functionality, you could just do this:
id newObj = // Acquire new object from wherever
while( newObj ){
if( ![arrayImConstructing containsObject:newObj] ){
[arrayImConstructing addObject:newObj]
}
newObj = // Acquire next object
}
This is basically what an ordered set has to do when you construct it anyways, and it's quite likely that your array is small enough (if you're putting it into a UIPicker) that you won't notice a performance difference at all. Still, measure the two, and then decide.
*NSOrderedSet is available on iOS > 5.0 (or OS X > 10.7).
NSOrderedSet gives you ordering in a set.
Why won't mapping change the array with this function:
array[1..5].map! { |part| "<p>#{part}</p>" }
I know that I could just assign array[1..5] to the result, but there's probably some better way to do it.
How should I do it?
[](*args)
Returns a new array populated with the given objects.
Source
So, you're actually applying map! to a new array of just that range, not the actual array.
Assignment is necessary.
Well, it does change the array. You're just not seeing it, because you never assign the array to anything, so it will be immediately garbage collected again.