Appending NSDictionary to other NSDictionary - objective-c

I have one NSDictionary and it loads up UITableView. If a user scrolls more and more, I call API and pull new data. This data is again in the form of an NSDictionary. Is it possible to add the new NSDictionary to the existing one?

You looking for this guy:
[NSMutableDictionary addEntriesFromDictionary:]
Make sure your UITableView dictionary is an NSMutableDictionary!
Check it here

Use NSMutableDictionary addEntriesFromDictionary to add the two dictionaries to a new mutable dictionary. You can then create an NSDictionary from the mutable one, but it's not usually necessary to have a dictionary non-mutable.

Is your NSDictionary full of other NSDictionaries? It sounds like you would need an NSMutableArray that you could add NSDictionaries to at the end. Assuming you can control the flow of data coming in and wouldn't run the risk of duplicates in your array, you could certainly append the data.
NSMutableArray *array = [[NSMutableArray alloc] init];
arrayCount = [array count]; // check the item count
[array addObject:dictToAppend];
Without seeing how you are implementing it, I can't provide more detailed code examples. Appending items is easy to do, but know it can only be done with mutable versions of Arrays or Dictionaries. Hope this helps a little.

Related

Add Array with Dictionaries to another Array in Objective-C

I want to add an Array which contains a Dictionary to an other Array. So the first Array contains a few items and each item consists of 4 key/value pairs. Now I want to add the first Array to the second on position [i,1] (so the second Array would be a three dimensional array?). How can I achieve this?
here's my code:
NSMutableArray *routenArray = [[NSMutableArray alloc] init];
NSMutableArray *firstArray = [[NSMutableArray alloc] init];
NSDictionary *firstDict = [NSDictionary dictionaryWithObjectsAndKeys:abfahrt,#"Abfahrt", ankunft, #"Ankunft", dauer, #"Dauer", route, #"Route", nil];
[firstArray addObject:firstDict];
for (int i = 0; i < firstArray.count; i++) {
routenArray [i][1] = firstArray [i];
}
You know, you really need to learn the basics of "3D" arrays, and indeed arrays / NSMutableArrays generally.
Fundamentally you use addObject to add an item to an NSMutableArray.
The answer to your question is that you use addObject to add objects to your "outside" array.
The things you add, can themselves me complex data objects like arrays - that's the "3D' part as you're thinking about it.
To access elements of an NSArray (or NSMutableArray), you use firstObject, lastObject, or objectAtIndex.
(You don't use braces[1][17].)
I encourage you to read some basic tutorials on NSArray for Xcode.
Click the "tick" sign to mark my answer as correct, closing this question. Then, forget you ever asked this question :) Read some tutorials, then ask new specific questions about NSArray. Also search here for 1000s of examples and QA.
It's possible you're looking for addObjectsFromArray
Generally to solve problems like this.
Go to the dock for the highest class involved in your problem, in this case NSMutableArray ...
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html
no matter how experienced you are, you can always find amazing stuff in there. Just read through each method available to you; often you find just what you want.
In this case addObjectsFromArray is your gift from above. Next, on this site just search on addObjectsFromArray and you'll find lots of great example code and other incidentalia.
Cheers!
There are no "three dimensional arrays". There is NSArray (and NSMutableArray), and they contain objects. Since NSArray and NSDictionary are themselves objects, an NSArray can contain another NSArray which can contain another NSArray. That doesn't make it three-dimensional. And that's where your problem comes from: "routenArray" (wer mixt hier deutsche und englische Namen? ) doesn't contain any objects. routenArray [0] is nil.
What you are writing:
routenArray [i][1] = firstArray [i];
is just a shortcut for
[[routenArray objectAtIndex:i] replaceObjectAtIndex:1 withObject:[firstArray objectAtIndex:i]];
So this is very much not what you think it is.

Why does accessing my NSMutableArray become faster once I've added more objects to it?

Something very odd is going on. I populate my array as follows:
self.workingWithItemCollectionArray = [NSMutableArray arrayWithCapacity:self.itemCollectionArray.count];
self.workingWithItemCollectionArray = [[self.itemCollectionArray mutableCopy]autorelease];
It take a mutable copy of the original NSArray and pass it to the NSMutableArray. When accessing the information contained in this array by the click of a UIButton, there is a slight delay in retrieving the information.
But when I then change the original array to add more items, and then pass this onto the mutable array:
NSMutableArray *editedOriginalArray = [NSMutableArray arrayWithArray:self.itemCollectionArray];
[editedOriginalArray addObjectsFromArray:extraObjectsToAdd];
self.itemCollectionArray = [NSArray arrayWithArray:editedOriginalArray];
self.workingWithItemCollectionArray = [NSMutableArray arrayWithCapacity:self.itemCollectionArray.count];
self.workingWithItemCollectionArray = [[self.itemCollectionArray mutableCopy]autorelease];
It is then after this that I am able to press the button and information is accessed instantly (whereas before the button would stay pressed for a very short time).
Any ideas on why this could be?
It has to do with how NSMutableArray is implemented vs NSArray.
Because NSArray is immutable, the objects are literally internally stored in an array, e.g.:
id *objects = malloc(sizeof(id) * count);
However, when you deal with NSMutableArray, you are dealing with instead, a linked list, as NSMutableArray expects many modifications on the array. So, the lookup on a linked list is much longer, because your objects are not stored in a way where there is a set distance in memory between them.
For more information on linked lists, check here.

addObject to NSMutableArray in NSMutableArray

I have my UITableView data organized such that the sections of the table are the array elements of a NSMutableArray. Each element itself is a NSMutablearray of NSMutableDictionary, representing the row of each section and the data for each row.
When I create this, I load all the data and create each section's NSMutableArray, then add them one at a time to the "outer" NSMutableArray. I reference the rows by:
[[[objData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:#"name"]
and have had no problems doing this. I think the advantage here is I can have a variable number of sections, which I need, and a variable number of rows in each section, also a requirement.
I do, however, get an error when I try to add a row to a section.
[[objData objectAtIndex:0] addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys: objectID,#"id",nameVar,#"name",nil]];
doesn't work. Is this the problem, or is there a problem with the way I've layed out the array? I'm thinking that the inner NSMutableArray is only alloted so much memory, but I would have thought the array is just an array of references and wouldn't have that issue. I could be wrong - it isn't the first time.
Thanks in advance for wrapping your head around this.
How are you intializing the array and the values inside it? I think you have an array that is not mutable.
Also an NSMutableArray has a variable size, calling initWithSize is completely unnecessary. The array will resize if it needs to.

How can I do a multidimensional lookup table in Objective-C?

New to Objective-C here...
I have a unique string code as the key that I'd like to use to look up associated values.
It seems an NSDictionary is what I'm looking for but I believe this is only used to lookup one value for a given key.
Can anyone provide the code on how to declare/fill a multidimensional immutable NSDictionary? Also the code to retrieve the values?
Also, please let me know I'm going about this the wrong way
Thanks
EDIT: example of array within a dictionary .... using example data from your comment.
NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init];
NSArray *firstSet = [[NSArray alloc] initWithObjects:43, 22];
NSArray *secondSet = [[NSArray alloc] initWithObjects:32, 50];
[myDictionary setValue:firstSet forKey:#"010"];
[myDictionary setValue:secondSet forKey:#"011"];
// Get a value out - should be the 50 that you want
NSInteger *myValue = [[myDictionary objectForKey:#"011"] objectAtIndex:1];
Not tested - but should be 95% right. Does this make sense?
You can make any object you want be the value for a given key in an NSDictionary. This includes NSArray or even another NSDictionary. Using either of these would allow you to associate multiple values with one key.
For nested NSDictionaries or custom KVC-complient classes you can use keyPaths, and for nested NSArrays indexPaths.
Also stackoverflow will give you many examples.

Quick NSMutableArray Question

I was wondering, would using a NSMutableArray be the best way for making an array that i will be adding objects to? Or, just a regular NSArray? secondly, I'm trying to make something sort of like an ArrayList in java (so there is no limit to the size), and I would like to know how to do that. What I've thought of is to make a bigger array and copy older array into it. My code:
- (void) addAccount:(BankAccount *)b
{
accountCount = [NSNumber numberWithDouble:[accountCount doubleValue] + 1];
NSMutableArray *oldList = accounts;
accounts = [[NSMutableArray alloc] (some code to make bigger and copy over)];
}
P.S. I taught myself this language yesterday, so I may not understand you response if it's too advanced
NSMutableArrays are what you want. Also, NSMutableArrays are already like ArrayLists or STL vectors, or anything else with "no limit to the size". You can say [myArray addObject:someObject]; until you run out of memory, and it will just keep resizing itself as needed.
The difference between an NSMutableArray and an NSArray lies in the meaning of the word "mutable". i.e.: A mutable array can be modified after it's created whereas a "normal" NSArray is immutable and can't be modified after it's created.
As such, using an NSMutableArray and adding objects to it via the addObject: method would seem an ideal solution.
If you want to be adding objects all at once use NSArray. If you're going to be adding some objects now, then more later, use NSMutableArray.
Your code snippet doesn't make much sense. To make an NSMutableArray, do this:
NSMutableArray *array = [NSMutableArray array];
If you don’t need an order (normally you don’t), use a NSSet/NSMutableSet.