Adding dictionaries to another - nsdictionary

a silly question:
How do i fit multiple dictionaries together in one or add one to an other?
i tried to initialize my dictionary with two other.. it didn't work.
dic1 = [[NSMutableDictionary alloc] initWithDictionary:dic2, dic3];
thank you

You want to use the addEntriesFromDictionary:dic3 method on NSMutableDictionary:
dic1 = [[NSMutableDictionary alloc] initWithDictionary:dic2];
[dic1 addEntriesFromDictionary:dic3];

Related

Only the last value of NSMutableDictionary is added to NSMutableArray

I have tried my best to search for the answers to add NSMutableDictionary to NSMutableArray. Unfortunately, they all asked me to use "[allValues]" which is not what I want.
Here is the issue,
In the header file, I defined,
NSMutableArray *arData;
And in the m file,
arData = [[NSMutableArray alloc] init];
NSMutableDictionary *mutableDictionary5 = [[NSMutableDictionary alloc]init];
for (i=0;i<[[[self rssParser]rssItems]count];i++)
{
Lat = [[[[[self rssParser]rssItems]objectAtIndex:i]lat] doubleValue];
Lng = [[[[[self rssParser]rssItems]objectAtIndex:i]lng] doubleValue];
// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(Lat, Lng);
marker.title = [[[[self rssParser]rssItems]objectAtIndex:i]header];
[mutableDictionary5 setObject:marker.title forKey:#"title"];
[arData setValue:[mutableDictionary5 valueForKey:#"title"] forKey:#"title"];
}
The "arData" only gets the last value of [[[[self rssParser]rssItems]objectAtIndex:i]header].And I am sure mutableDictionary5 gets all the value since I have put the following at the end of the code,
NSLog(#"mutableDictionary5 values=%#",[mutableDictionary5 allValues]);
It prints all the value of the title.
I understand it shouldn't be setvalue, however, I tried to use [mutableDictionary5 allValues], "addobjects", "allkeys". They are not working. What should I do to let the NSMutableArray arData to add all values of title with "title" as the key? thanks.
Sorry for my bad English which is not my native language.
You erroneously use setValue instead of addObject.
[arData addObject:mutableDictionary5];
This will work if you alloc init a new dictionary inside the loop.
If you want to reduce the array to just titles do this after the loop:
NSArray *titleArray = [arData valueForKeyPath:#"title"];
Now you have an array containing just titles.
BTW, you could make some effort to invent better variable names. Note that by convention in Objective-C variable names start with low cap while class names are capitalized.

How can i add three objects into one in mutableArray in iphone application?

NSMutableArray * mutableArra = [[NSMutableArray alloc] init];
[mutableArra addObjectsFromArray:myArray];
NSLog(#"=====mutable aarrya ====::%#",mutableArra);
This is the result from the previous code:
01,12,2012
How can I add these three objects into one like the following?
01-12-2012
Thank you in advance.
I'm not sure how do you want to "add" those poor objects to one another, but if you want them to be concatenated as a string, try:
NSString *result = [myArray componentsJoinedByString:#"-"];
If this is for dates, I would recommend looking into NSDate which will do the formatting for you.

How to declare 2D array in Objective-C?

I am new to iPhone. I tried a lot to declare 2D array but I am unable to get the result.
NSMutableArray *outerarray = [[NSMutableArray alloc] init];
outerarray = [NSMutableArray arrayWithObjects:
[NSMutableArray arrayWithObjects:#"10-20","21-30","31-40","41-50","51-60","61-70","71-80","81-90","91-100",nil,
[NSMutableArray arrayWithObjects:#"10-20","21-30","31-40","41-50","51-60","61-70","71-80","81-90","91-100",nil],
nil];
I want to combine two arrays elements and compare it with other element.
Anyone can help me please?
#"..." is an NSString. "..." is not, it's a C string constant. You need to use the #"..." syntax for all NSString objects you're putting into an NSArray.
NS[Mutable]Array does not support 2D as far as I know. You can probably jury-rig it to mutate and access elements in 2D (make that its own class). Once you can access arbitrary elements, you can "combine" them and compare them as you need. The alternative is since Objective-C is a superset of C, its int arrayName [][] is available (but discouraged).

How to create an array with Dictionaries?

I just need some help in creating an array which can store multiple dictionaries.
I have :
.h
NSArray * projects;
NSDictionary *project1;
NSDictionary * project2;
.m
projects = /* What should I write here to store the
above two dictionaries in my array. */
Thanks,
projects = [[NSArray alloc] initWithObjects:project1, project2, nil];
Basically (and this will be right or not quite right depending on you actual code, declarations, properties, etc). Because you declared projects as an NSArray, you could do the following:
projects = [NSArray arrayWithObjects: project1, project2, nil];
This variable will go away magically when it goes out of scope unless you retain it.
Alternatively, if you declare projects as an NSMutableArray and make a property with attributes (retain, nonatomic), you could do something like this:
NSMutableArray *a = [[NSMutableArray alloc] initWithObjects:project1, project2, nil];
self.projects = a;
[a release];
There are several ways to achieve your goal, depending on your overall needs for the array.
I'm not sure if what you are looking for is more complex then this, but:
projects = [[NSArray alloc ] initWithObjects:project1,project2, nil];
should work.
If you are after something more complex then this let me know I will revise my answer.
arr = [[NSArray alloc ] initWithObjects:dict1,dict2, nil];

iPhone - Merge keys in a NSArray (Objective-C)

I'm having troubles with arrays and keys... I have an array from my database:
NSArray *elementArray = [[[menuArray valueForKey:#"meals"] valueForKey:#"recipe"] valueForKey:#"elements"]
The problem here is that I would like all my elements of all my meals of all my menus in an array such that:
[elementArray objectAtIndex:0] = my first element
etc...
In the example above, the elements are separated by the keys.
How can I get that?
Hope it's clear enough...
Thanks
From your code snippet, it is not clear to me exactly how your data is structured, but I think it's analogous to having an NSDictionary (called aDictionary) of NSArray and wanting to combine all the NSArray into one. If this is the case, then:
NSMutableArray *resultArray = [[[NSMutableArray alloc] init] autorelease];
for (id dictionaryKey in aDictionary) {
[resultArray addObjectsFromArray:[aDictionary objectForKey:dictionaryKey]];
}
return [NSArray arrayWithArray:resultArray];
(This code has not been tested.)