replace object at indexPath - big array with 12 sections - ios

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
);
}
)

Related

how do i add these dict in one single array

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:#{#"Name": self.txt1.text , #"Roll No.":self.txt2.text,}];
NSMutableDictionary *dict1= [NSMutableDictionary dictionaryWithDictionary:#{#"Name":self.txt3.text , #"Roll No.":self.txt4.text}];
NSMutableDictionary *dict2 = [NSMutableDictionary dictionaryWithDictionary:#{#"Name":self.txt5.text , #"Roll No.":self.txt6.text}];
NSMutableDictionary *dict3 = [NSMutableDictionary dictionaryWithDictionary:#{#"Name":self.txt7.text, #"Roll No.":self.txt8.text}];
How do i add these dictionary in single array...
Add objects(dictionaries) in array as following:-
NSArray *arrayData = [[NSArray alloc]initWithObjects:dict,dict1,dict2,dict3, nil];
//Show values in other view controller (as per comment)NSDictionary :-
Traverse the array, get the values, show the values in labels(IBOutlets)
*dicData = [arrayData objectAtIndex:0];
NSString *name = [dicData objectForKey:#"Name"];
NSString *rollNo = [dicData objectForKey:#"Roll No."];
labelName.text = name;
labelRollNo.text = rollNo;
attributes = [[NSMutableArray alloc] init];
[attributes addObject: dict.mutableCopy];
[attributes addObject: dict1.mutableCopy];
[attributes addObject: dict2.mutableCopy];
[attributes addObject: dict3.mutableCopy];
that's it
Try this
NSMutableArray *arrDict = [NSMutableArray alloc]initWithObjects:#{#"Name" : self.txt1.text , #"Roll No" : self.txt2.text},#{#"Name" : self.txt7.text, #"Roll No" : self.txt8.text} nil
Using NSArray Literal
NSArray *arrDict =#[#{#"Name" : self.txt1.text , #"Roll No" : self.txt2.text},#{#"Name" : self.txt7.text, #"Roll No" : self.txt8.text} ];
In Next ViewController you can get this dictionary through loop
for (NSDictionary *dicUser in arrDict) {
// this dictionary which contained by arrDict
UILabel *lblName = [dicUser objectForKey:#"Name"];
UILabel *lblRollNumber = [dicUser objectForKey:#"Roll No"];
}

create JSON from 2 arrays in IOS

I'm trying to create dictionary to POST JSON data to server:
NSArray *keys = [NSArray arrayWithObjects:#"lat", #"lon", nil];
NSArray *values = [NSArray arrayWithObjects: orderClass.extraLat, orderClass.extraLon, nil];
NSDictionary *postDict = [NSDictionary dictionaryWithObjects:values forKeys:keys];
this gives me:
{
lat = (
"54.720746",
"54.719206",
"54.717466"
);
lon = (
"56.011108",
"56.008510",
"56.007031"
);
}
But the aim is to POST data from arrays in format:
[{"lat":"54.720746", "lon":"56.011108" },
{ "lat":"54.719206", "lon":"56.008510"},
{ "lat":"54.717466", "lon":"56.007031"}]
Need your help.
Thanks for paying attention!
As I said in the comment - you need to reverse the steps towards your goal. First dictionaries, then array. And you'll get what you want.
NSArray *keys = [NSArray arrayWithObjects:#"lat", #"lon", nil];
NSArray *lats = [NSArray arrayWithObjects:#"1", #"2", #"3", nil];
NSArray *lons = [NSArray arrayWithObjects:#"4", #"5", #"6", nil];
// your way (not what you want)
NSArray *values = [NSArray arrayWithObjects: lats, lons, nil];
NSDictionary *postDict = [NSDictionary dictionaryWithObjects:values forKeys:keys];
// my recommendation based on what you want
NSMutableArray *postData = [[NSMutableArray alloc] init];
for (int i = 0; i < lats.count; i++) {
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:[lats objectAtIndex:i] forKey:#"lat"];
[dict setObject:[lons objectAtIndex:i] forKey:#"lon"];
[postData addObject:dict];
}
This will get you these results:
(lldb) po postDict
{
lat = (
1,
2,
3
);
lon = (
4,
5,
6
);
}
And
(lldb) po postData
<__NSArrayM 0x7ffb5be4d950>(
{
lat = 1;
lon = 4;
},
{
lat = 2;
lon = 5;
},
{
lat = 3;
lon = 6;
}
)

XML-RPC request Wordpress

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

iOS: value not coming as String?

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"]];

how to update NSMutableArray data depending on id or based on position

I have NSMutableArray data as below.
(
{
Id = 3;
Name = Fahim;
},
{
Id = 2;
Name = milad;
},
{
Id = 1;
Name = Test;
}
)
Now I want to update the name from Test to Omar (for id = 1).
Any idea how to get this done?
Answer
With below answer, I was getting error as -[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance. To resolve that issue I Changed [feeds addObject:[item copy]] to [feeds addObject:item]
for (NSMutableDictionary* aDict in yourMutableArray) {
if (aDict[#"id"] == 1) {
[aDict setObject:#"Omar" forKey:#"Name"];
}
}
EDIT :
NSMutableArray* mutableArray = [[NSMutableArray alloc]init];
NSDictionary* item1Dict = [NSDictionary dictionaryWithObjectsAndKeys:
#"1",#"id",
#"Fahim",#"name"
, nil];
NSMutableDictionary* item1 = [NSMutableDictionary dictionaryWithDictionary:item1Dict];
[mutableArray addObject:item1];
NSDictionary* item2Dict = [NSDictionary dictionaryWithObjectsAndKeys:
#"2",#"id",
#"milad",#"name"
, nil];
NSMutableDictionary* item2 = [NSMutableDictionary dictionaryWithDictionary:item2Dict];
[mutableArray addObject:item2];
NSDictionary* item3Dict = [NSDictionary dictionaryWithObjectsAndKeys:
#"3",#"id",
#"test",#"name"
, nil];
NSMutableDictionary* item3 = [NSMutableDictionary dictionaryWithDictionary:item3Dict];
[mutableArray addObject:item3];
NSLog(#"%#",mutableArray);
for (NSMutableDictionary* aDict in mutableArray) {
if ([aDict[#"id"] isEqualToString:#"3"]) {
[aDict setObject:#"Omar" forKey:#"name"];
}
}
NSLog(#"%#",mutableArray);
And for much elegant:
NSMutableArray* mutableArray = [[NSMutableArray alloc]init];
NSArray* name = [NSArray arrayWithObjects:#"Fahin",#"milad",#"test", nil];
for (int i = 0; i < name.count; i++) {
NSDictionary* itemd = [NSDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithFormat:#"%i",i],#"id",
name[i],#"name"
, nil];
NSMutableDictionary* item = [NSMutableDictionary dictionaryWithDictionary:itemd];
//or
//NSMutableDictionary* item = [item mutableCopy];
[mutableArray addObject:item];
}
NSLog(#"%#",mutableArray);
for (NSMutableDictionary* aDict in mutableArray) {
if ([aDict[#"id"] isEqualToString:#"2"]) {
[aDict setObject:#"Omar" forKey:#"name"];
}
}
NSLog(#"%#",mutableArray);
logs are:
2013-10-18 00:51:48.845 test[34919:60b] (
{
id = 1;
name = Fahim;
},
{
id = 2;
name = milad;
},
{
id = 3;
name = test;
}
)
second log:
2013-10-18 00:52:06.887 test[34919:60b] (
{
id = 1;
name = Fahim;
},
{
id = 2;
name = milad;
},
{
id = 3;
name = Omar;
}
)
EDIT2 for copy:
-copy, as implemented by mutable Cocoa classes, always returns their immutable counterparts. When an NSMutableDictionary is sent -copy, it returns an NSDictionary containing the same objects. NSMutableDictionary is a subclass of NSDictionary, the compiler doesn't complain. NSDictionary does not recognize it's mutable subclass' methods (because it cannot mutate it's contents).

Resources