I need to append strings to an array kept inside of a dictionary each time a word matches a pattern of another word. The second line below (setObject) overwrites instead of appending.
The end result should be arrays in a dictionary where the the key(pattern) identifies many strings that fit that pattern.
NSMutableDictionary *eqClasses = [[NSMutableDictionary alloc] init];
[eqClasses setObject:tempWordStr forKey:wordPattern];
Is there an easy way to append?
Try this:
NSMutableArray* array = [eqClasses objectForKey:wordPattern];
if(!array) {
// create new array and add to dictionary if wordPattern not found
array = [NSMutableArray array];
[eqClasses setObject:array forKey:wordPattern];
}
[array addObject:tempWordStr];
You indicate that the values in the dictionary should be arrays, but it looks to me like eqClasses contains NSStrings (tempWordStr). Don't you need to create an NSArray to hold the NSStrings associated with a keyword and then make the array the value in the dictionary that corresponds to the keyword? If the dictionary already contains the key, you need to retrieve the array associated with the key, add the new string to the array, and then call setObject using the array with the key.
Related
I have an array, that consist of following strings:
[NSString stringByAppendingFormat:#"<p style=\"padding-left:20px;margin-bottom:-10px;\"><i>%#%#%#%#</i></p>",
wrappingBy, pack1, pack2, strFirmName];
For example, it have 200 different strings. First parameter - wrappingBy, may have several different names. For example - box, tube, bag, etc.
What i want is, to enumerate through that array, and create different arrays depending on that name. So, if my array consist of 50 strings start from box, 50 strings start from tube, and 100 strings start from bag i want 3 different arrays.
Is there any easy way to achieve that?
Try this:
Let's say you have the array of strings:
NSArray *arr=#[#"box3523sfgsg",#"boxsdfsdf3",#"bag!#$#",#"!##4bag",#"tube##$FR",#"tubeASAD"];
In your case, the above array is filled with following string
[NSString stringByAppendingFormat:#"<p style=\"padding-left:20px;margin-bottom:-10px;\"><i>%#%#%#%#</i></p>",
wrappingBy, pack1, pack2, strFirmName];
Now add the wrappingBy param to the array everytime you add the above string to the array, and make sure you don;t add duplicates to the array. YOu can check the duplicates before adding them to the array.
and in your case, you would do
NSMutableArray *arrayNAme=[[NSMutableArray alloc]init];
//make arrayName mutable
if (![arrayNAme containsObject: wrappingBy]) {
[arrayNAme addObject: wrappingBy];
}
you will get arrayNAMe contain following:
arrayNAme=#[#"box",#"bag",#"tube"];
Now search the main array string if it contains the wrapingBy names or not, if YES, add them to an array and add that array to the dicitonary:
NSMutableDictionary *myDictionary=[[NSMutableDictionary alloc]init];
for( NSString *nameString in arrayNAme) {
NSMutableArray *strnArray=[[NSMutableArray alloc]init];
for (NSString *str in arr)
{
if([str containsString:nameString])
{
[strnArray addObject:str];
[myDictionary setValue:strnArray forKey:nameString];
}
}
}
At the end you have the dictionary:
{
bag = (
"bag!#$#",
"!##4bag"
);
box = (
box3523sfgsg,
boxsdfsdf3
);
tube = (
"tube##$FR",
tubeASAD
);
}
Now you can get each key value and store them in a separate array.
I am dynamically populating a NSMutableDictionary with keys that are identical. However doing so replaces the original keys value. What I require is it to be appended and not replacing the existing key. For example, I need a structure like
{
#"Key" : #"Value1",
#"Key" : #"Value2",
#"Key" : #"Value3"
}
I know I could add each NSDictionary that is created to a NSMutableArray but my issue comes because I need the input value to be a NSDictionary.
Currently I have the following which replaces the original value
NSMutableDictionary *ripDictionary = [[NSMutableDictionary alloc] init];
for(NSString *ripId in recievedRips){
//SOME OTHER CODE
ripDictionary[#"rip"] = keysAndAttributes;
[data addObject:ripDictionary];
}
From the NSDictionary Reference
A key-value pair within a dictionary is called an 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:).
Perhaps you could modify your code to accept a dictionary like:
{
#"Key" : [
#"Value1",
#"Value2",
#"Value3"
]
}
You can only have a single unique key per dictionary, so if you want multiple values associated with it then you would add those values to an array associated with the key.
if([aDictionary objectForKey:#"key"] != nil){
aDictionary[#"key"] = #[aDictionary[#"key"], bDictionary[#"key"]];
}else{
aDictionary[#"key"] = bDictionary[#"key"];
//OR make all aDictionary values array by default with a single value
//but you get the point
}
I have an array and it has lots of dictionary's keys it comes from API. My array as follows
Dictionary keys array :
NSArray *arr = #[#"01", #"02", #"03"];
Dictionary with key-value pairs
NSDictionary *dic = #{#"01": #"Hero", #"02" : #"Enemy", #"03" : #"Boss"};
Basically i want to match array values corresponding to dictonary keys without using array. I found a solition about that but I don't want to use for-loop for every cell(I have a lots of cell). My solution is like that
for(NSString *item in arr) {
[convertedArr addObject:[dic valueForKey:item]];
}
NSLog(#"%#", [convertedArr componentsJoinedByString:#","]);
Asumme have an array like this (1,2,3) and dictionary looks like {1 = "a", 2 = "b", 3 = "c"} I just want to give an array and it should return dictionary values like this ("a","b","c")
Anybody should give me better approach without using array? Thanks.
You can replace your for-loop by
NSArray *convertedArr = [dic objectsForKeys:arr notFoundMarker:#""];
which is at least less code. (The notFoundMarker: is added for all keys
in the array which are not present in the dictionary. Your code would crash
in that situation.)
It might perform slightly better because it is a library
function. But I doubt that the difference is big, because in any case a dictionary
loopup is required for all keys in arr.
I am getting a response from a server and I am saving it in a dictionary like this, I am using an NSMutableArray for this purpose.
{
a = "";
b = "";
c = "";
d = "";
}
I want to print the keys in the same order as they are returned from the server.
But when I print the keys the order is: c, d, a, b.
The code I am using is:
for(id key in dic){
nslog(#"%#",key);
}
How can I do this correctly?
By nature NSDictionary and NSMutableDictionary are unordered. You would not be able to do it this way. I would suggest storing the keys into an NSArray or NSMutableArray and then printing out the elements of the array in order.
Try to keep the response in an NSMutableArray, where each element is a simple NSDictionary with only one key-value pair.
NSMutableDictionary is unordered collection.You can make your own ordered dictionary subclass and adding the keys to array in order they come.
Have a look here is nice explanation how to make ordered dictionary.You can also use this link
NSDictionary and NSMutableDictionary are unordered collections.
But you can use OrderedDictionary.
I am deserializing data into a dictionary, but the result is a nested dictionary so it looks like:
{"0":{"key1":"value1","key2":"value2"},
{"1":{"key1":"value1","key2":"value2"},
{"2":{"key1":"value1","key2":"value2"}}
I want to use this in a tableview so how would I go about turning that dictionary into an Array where each objet in the array is a dictionary with the value pairs?
I want to be able to step through the array, and reference the value pairs for each row of the array.
Thanks!
for (NSDictionary *dict in outerDictionary.allValues) {
[tableArray addObject:dict];
}
Indeed, or simpler:
tableArray = outerDictionary.allValues;