I have an NSMutableArray that contains several NSMutableDictionary object, each dict has a NSString and an NSMutableArray in it.
My problem is that I need to find the correct dict based on a string match and when found insert an object into the NSMutableArray in the same dict where the string match was found, I can only make this work the first time when the array is empty because after that I am unable to match the correct string:
Here is what I have tried so far, I tried using contains object but that wont get me inside the dict inside the array so I am stuck
NSMutableArray *variantOptions = [[NSMutableArray alloc] init];
for (NSDictionary *variant in variantInfo) {
NSMutableDictionary *variantOption = [[NSMutableDictionary alloc] init];
NSMutableArray *variantOptionValues = [[NSMutableArray alloc] init];
NSString *name = variant[#"name"];
NSString *value = variant[#"value"];
if(variantOptions.count > 0) {
// Need to loop through the array until name isEquaToString variantOptionName and when true insert value into variantOptionValuesArray in that same dict and if not exists create a new dict and insert in array
} else {
[variantOptionValues addObject:value];
[variantOption setObject:name forKey:#"variantOptionName"];
[variantOption setObject:variantOptionValues forKey:#"variantOptionValues"];
}
[variantOptions addObject:variantOption];
}
for (NSDictionary *variant in variantInfo) {
NSString *name = variant[#"name"];
NSString *value = variant[#"value"];
BOOL found = false;
for (NSMutableDictionary *v in variantOptions) {
if (v[#"name"] isequalToString:name]) {
NSMutableArray *values = v[#"variantOptionValues"];
[values addObject:value];
found = true;
break;
}
}
if (!found) {
// name was not found
NSMutableDictionary *variantOption = [[NSMutableDictionary alloc] init];
NSMutableArray *variantOptionValues = [NSMutableArray alloc] init];
[variantOptionValues addObject:value];
[variantOption setObject:name forKey:#"variantOptionName"];
[variantOption setObject:variantOptionValues forKey:#"variantOptionValues"];
[variantOptions addObject:variantOption];
}
}
i have an array with 12 sections and i need to replace value at index.
My test code:
NSMutableArray *hm = [[NSMutableArray alloc] initWithObjects:#{#"first": #[#"test1", #"test2"]}, #{#"second": #[#"test1"]}, nil];
NSLog(#"%#", [hm valueForKey:#"first"][0][0] );
[[hm valueForKey:#"first"][0] replaceObjectAtIndex:0 withObject:#"lol"];
NSLog(#"%#", hm);
First NSLog returns : test1 - its ok
When replace - crash with -[__NSArrayI replaceObjectAtIndex:withObject:]: unrecognized selector sent to instance 0x7fde53d2f700
I need to change test1 to something.
Wha am i doing wrong please?
NSMutableArray *hm = [[NSMutableArray alloc] initWithObjects:#{#"first": #[#"test1", #"test2"]}, #{#"second": #[#"test1"]}, nil];
NSLog(#"%#", [hm valueForKey:#"first"][0][0] );
//Your inner array is immutable, change it to mutable and replace the object, That's it.
NSMutableArray *array = [[hm valueForKey:#"first"][0] mutableCopy];
[array replaceObjectAtIndex:0 withObject:#"lol"];
[hm replaceObjectAtIndex:0 withObject:array];
NSLog(#"%#", hm);
NSMutableArray *arr = [[NSMutableArray alloc] initwithArray[[hm objectAtIndex:0]objectForKey:#"first"]];
[arr replaceObjectAtIndex:0 withObject:#"lol"];
[hm replaceObjectAtIndex:0 withObject:arr];
You have to make mutable dictionary and array structure
NSMutableArray* names = [NSMutableArray arrayWithObjects:
[NSMutableDictionary dictionaryWithObjectsAndKeys:
#"Joe",#"firstname",
#"Bloggs",#"surname",
nil],
[NSMutableDictionary dictionaryWithObjectsAndKeys:
#"Simon",#"firstname",
#"Templar",#"surname",
nil],
[NSMutableDictionary dictionaryWithObjectsAndKeys:
#"Amelia",#"firstname",
#"Pond",#"surname",
nil],
nil];
NSLog(#"Before - %#",names);
[names replaceObjectAtIndex:0 withObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:
#"Joe_New",#"firstname",
#"Bloggs_New",#"surname",
nil]];
NSLog(#"After - %#",names);
Before - (
{
firstname = Joe;
surname = Bloggs;
},
{
firstname = Simon;
surname = Templar;
},
{
firstname = Amelia;
surname = Pond;
}
)
After - (
{
firstname = "Joe_New";
surname = "Bloggs_New";
},
{
firstname = Simon;
surname = Templar;
},
{
firstname = Amelia;
surname = Pond;
}
)
NSMutableArray *hm = [[NSMutableArray alloc] initWithObjects:#{#"first": #[#"test1", #"test2"]}, #{#"second": #[#"test1"]}, nil];
NSMutableDictionary *dic=[[NSMutableDictionary alloc] initWithDictionary:[hm objectAtIndex:0]];
NSMutableArray *array=[NSMutableArray arrayWithArray:[dic objectForKey:#"first"]];
[array replaceObjectAtIndex:0 withObject:#"lol"];
[dic setObject:array forKey:#"first"];
[hm replaceObjectAtIndex:0 withObject:array];
O/P (ViewController.m:48) (
(
lol,
test2
),
{
second = (
test1
);
}
)
I have no problems to get the value of name in terms (name of my blog post category) out of the xmlrpc server response object ...
Server response:
responseArray: (
{ guid = "http://www.domain.com/wp/?p=12";
"post_id" = "123";
terms = (
{ name = "Uncategorized"; }
);
}
)
... with the following lines of Objective-C code:
NSMutableArray *responseArray = [[NSMutableArray alloc] init];
responseArray = [nodeSaveResponse object];
for(i = 0; i < responseArray.count; i++) {
NSString *postLink = [[responseArray objectAtIndex: i] valueForKey: #"guid"];
NSString *postId = [[responseArray objectAtIndex: i] valueForKey: #"post_id"];
NSMutableArray *catArray = [[NSMutableArray alloc] init];
catArray = [[responseArray objectAtIndex: i] valueForKey: #"terms"]];
NSArray *cat = [catArray valueForKey: #"name"];
NSString *myCatString = [cat objectAtIndex: 0];
}
But to send a new blog post fails because the following code to pack the category string is somehow wrong:
NSString *myCatString = #"MyCategory";
NSMutableDictionary *name = [[NSMutableDictionary dictionaryWithObjectsAndKeys: myCatString, #"name", nil];
NSMutableArray *catArray = [[NSMutableArray arrayWithObject: name];
NSMutableDictionary *values = [[NSMutableDictionary alloc] init];
[values setObject: title forKey: #"post_title"];
// and so on with other values - until here everything works well
[values setObject: catArray forKey: #"terms"]; // if this line is called, the request fails
NSArray *params = [NSArray arrayWithObjects: #"1", user, pass, values, nil];
[myRequest setMethod: #"wp.newPost" withParameter: params];
Any idea, where my fault is?
Cheers, Martin
In my code, I set array inside a NSMutableDictionary as
if (array.count > 0) {
[self.filters setValue:array forKey:[self getKey:[[NSNumber numberWithInt:indexPath.section] intValue]]];
}
where array is
NSMutableArray *array = [[NSMutableArray alloc] init];
When the receiving code receives it, I tried to join the values in array as
if ([item objectForKey:#"category_filter"] != nil) {
NSArray *array = [NSArray arrayWithObjects:[item objectForKey:#"category_filter"], nil];
NSString *categories = [array componentsJoinedByString:#","];
NSLog(#"value:%#", categories);
}
where item is (NSMutableDictionary *)item
When I see log, I see as
2014-06-24 17:43:12.520 yelp[69744:70b] value:(
"Bagels (bagels)",
"Bakeries (bakeries)"
)
so they are not joined yet. What am I doing wrong here?
As Hot Licks commented,
I had to make change as
NSArray *array = [NSArray arrayWithArray:[item objectForKey:#"category_filter"]];
instead of
NSArray *array = [NSArray arrayWithObjects:[item objectForKey:#"category_filter"]];
UPDATE
Whenever I comment out these lines writeToFile will create a file but if I dont,it will not work.
Here is my code..
NSMutableDictionary *allData = [[NSMutableDictionary alloc] init];
NSArray *countries = [self retrieveCountries];
for(NSDictionary *dicCountry in countries){
NSString *countryName = [dicCountry objectForKey:#"en_name"];
NSArray *capital = [self retrieveCapitals:countryName];
NSMutableDictionary *capitalInfo = [[NSMutableDictionary alloc] init];
for(NSDictionary *dictPerCap in capital){
NSString *cap = [dictPerCap objectForKey:#"en_name"];
NSArray *items = [self retrieveItems:cap];
NSArray *categories = [self retrieveCategories:cap];
if(items == nil)
items = [[NSArray alloc] init];
if(categories == nil)
categories = [[NSArray alloc] init];
// store data in a dictionary
NSDictionary* tempCapDic = [NSDictionary dictionaryWithObjectsAndKeys:
items, #"items",
categories, #"categories",
nil];
//store
[capitalInfo setObject:tempCapDic forKey:cap];
}
// store data in a dictionary
NSDictionary* dataDic = [NSDictionary dictionaryWithObjectsAndKeys:
capital, #"capitals",
capitalInfo, #"info",
nil];
//store
[allData setObject:dataDic forKey:countryName];
}
/**************************/
NSDictionary *rootDict = [NSDictionary dictionaryWithObjectsAndKeys:countries, #"detailedCountries", allData, #"all", nil];
The lines that needs to be commented out inorder to work are:
capital, #"capitals",
capitalInfo, #"info",
in NSDictionary* tempCapDic.
Any idea on how to fix this?