How can I do a multidimensional lookup table in Objective-C? - 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.

Related

Objective-C: How to append uniques items from an array to another array?

I have an array and would like to append N items from another array to it, but only the items not already exist in the current array.
Note, the uniqueness of item is determined not by the object memory but its content. For example, I can have two distinct objects called Person with name "David" and I only one of this in my final result.
What's an efficient way to do this? I have looked at the options of doing it using NSPredicate and NSOrderedSet.
[#[arrayOne,arrayTwo] valueForKeyPath:#"#distinctUnionOfArrays.name"] where name is the property to merge the arrays with.
See NSHipster's KVC Collection Operators
Can be easily achieved using NSSet. Here is an example solution:
//Lines of init can be ignored. Just pay attention to the variables and their comments
NSArray * old = [[NSArray alloc]init]; //Assume this is your old array
NSArray * more = [[NSArray alloc]init]; //Assume this is your extra array
NSArray * new = [[NSArray alloc]init]; //Assume this is your new array (having duplicates)
new = [old arrayByAddingObjectsFromArray:more];
NSArray *cleanedArray = [[NSSet setWithArray:new] allObjects];
For explanation, NSSet automatically removes duplicates.
Warning: It does not preserve the order. If you want to proceed to maintain the order, sort it afterwards, or there are more complex solution on the way.

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.

extracting properties from NSArray of objects

Is there any of way (other than looping) of extracting a particular property of all objects in an array. So say there in an array of people. I want to extract all their first names into an array.
Key Value coding will help you with that:
NSArray *result = [people valueForKey:#"firstname"];
I got answer for my question.
This is how we can achieve the same in swift.
let arraytWithProperties = arrayWithObjects.map{ $0.propertyName }

Appending NSDictionary to other NSDictionary

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.

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.