I need to convert an NSMutableArray to an NSArray from JSON. I loaded the JSON and put it in an NSMutableArray.
NSMutableArray *banner = [[NSMutableArray alloc] init];
banner = [responseObject objectForKey:#"banner"];
This is test for loop:
for(int i = 0; i < [slider count]; i++) {
NSLog(#"%#", [[banner objectAtIndex:i] objectForKey:#"resim"]); }
This is log from the JSON:
(
{
id = 1;
resim = "http://localhost/sample_Files/banner_api/1.jpg";
},
{
id = 2;
resim = "http://localhost/sample_Files/banner_api/2.jpg";
},
{
id = 3;
resim = "http://localhost/sample_Files/banner_api/3.jpg";
}
)
I want to set all of this to be like the below NSArray:
NSArray *allImages = #[#"http://localhost/sample_Files/banner_api/1.jpg",#"http://localhost/sample_Files/banner_api/2.jpg",#"http://localhost/sample_Files/banner_api/3.jpg"];
How can I do this?
NSMutableArray * muArr = [NSMutableArray new];
for(NSDictionary * dict in yourMuArr) {
[muArr addObject:dict[#"resim"]];
}
NSArray * allImages = [NSArray arrayWithArray:muArr];
Related
I know this may be a repeated question but I googled a lot but not able to find a suitable answer for me.
I have a NSMutableArray which has two NSDictionary with Keys and values which I need to populated on a UITableView. I have retrieved the value of the dictionary which I'm going populate using
NSMutableArray *mutArray = [responseArray valueForKey:#"Table"];
And I did like
NSMutableSet *names = [NSMutableSet set];
NSMutableArray *mutArray1 = [[NSMutableArray alloc] init];
for (id obj in mutArray) {
NSString *destinationName = [obj valueForKey:#"AssetClassName"];
if (![names containsObject:destinationName]) {
[mutArray1 addObject:destinationName];
[names addObject:destinationName];
}
}
Because the value AssetClassName is repeated. Now I have three values in mutArray1 which I need to show as UITableView section. Under AssetClassName I have Some data which determines the row in that section.
For retrieving that data I'm doing like
for (int i = 0; i < [mutArray1 count]; i++) {
NSMutableDictionary *a = [[NSMutableDictionary alloc] init];
NSMutableDictionary *b = [[NSMutableDictionary alloc] init];
for (NSDictionary *dict in mutArray) {
if ([[mutArray1 objectAtIndex:i] isEqualToString:[dict valueForKey:#"AssetClassName"]]) {
[a setObject:[dict objectForKey: #"SubAssetClassName"] forKey:#"Investment Categories"];
[a setObject:[dict valueForKey:#"Amount"] forKey:#"Amount (EUR)"];
[a setObject:[dict valueForKey:#"AllocationPercentage"] forKey:#"%"];
[a setObject:[dict valueForKey:#"ModelAllocationPercentage"] forKey:#"ModelAllocationPercentage"];
[b setObject:a forKey:[dict valueForKey:#"SubAssetClassName"]];
[mutdict setObject:b forKey:[dict valueForKey:#"AssetClassName"]];
}
}
}
mutdict is a NSMutableDictionary declared globally and is instantiate in viewdidLoad
mutdict = [[NSMutableDictionary alloc] init];
The values are inserted into mutdict as I needed. Each SubAssetClassName is added into AssetclassName accordingly.
But my problem is in my final dictionary i.e mutdict the values for SubAssetClassName is repeated.
Can anybody tell how to solve this.
My console
"AssetClassName" = {
"SubAssetClass" = {
"%" = 0;
"Amount (EUR)" = 0;
"Investment Categories" = "HIGH YIELD BONDS";
"ModelAllocationPercentage" = 22;
};
"SubAssetClass" = {
"%" = 0;
"Amount (EUR)" = 0;
"Investment Categories" = "HIGH YIELD BONDS";
"ModelAllocationPercentage" = 22;
};
"SubAssetClass" = {
"%" = 0;
"Amount (EUR)" = 0;
"Investment Categories" = "HIGH YIELD BONDS";
"ModelAllocationPercentage" = 22;
};
};
"AssetClassName" = {
"SubAssetClass" = {
"%" = 0;
"Amount (EUR)" = 0;
"Investment Categories" = "EMERGING MARKETS EQUITIES";
"ModelAllocationPercentage" = 10;
};
};
"AssetClassName" = {
"SubAssetClass" = {
"%" = 0;
"Amount (EUR)" = 0;
"Investment Categories" = "STRUCTURED PRODUCTS";
"ModelAllocationPercentage" = 10;
};
"SubAssetClass" = {
"%" = 0;
"Amount (EUR)" = 0;
"Investment Categories" = "STRUCTURED PRODUCTS";
"ModelAllocationPercentage" = 10;
};
"SubAssetClass" = {
"%" = 0;
"Amount (EUR)" = 0;
"Investment Categories" = "STRUCTURED PRODUCTS";
"ModelAllocationPercentage" = 10;
};
};
}
Here I can see that all SubAssetClass values are same for each section but actually its not.
How can I solve this.
You need to create a new instance of your mutable dictionary inside the loop. Right now you create one instance and update it over and over. This results in one dictionary being added over and over.
Change you code as follows:
for (NSInteger i = 0; i < [mutArray1 count]; i++) {
NSMutableDictionary *b = [[NSMutableDictionary alloc] init];
for (NSDictionary *dict in mutArray) {
if ([[mutArray1 objectAtIndex:i] isEqualToString:[dict valueForKey:#"AssetClassName"]]) {
NSMutableDictionary *a = [[NSMutableDictionary alloc] init];
[a setObject:[dict objectForKey: #"SubAssetClassName"] forKey:#"Investment Categories"];
[a setObject:[dict valueForKey:#"Amount"] forKey:#"Amount (EUR)"];
[a setObject:[dict valueForKey:#"AllocationPercentage"] forKey:#"%"];
[a setObject:[dict valueForKey:#"ModelAllocationPercentage"] forKey:#"ModelAllocationPercentage"];
[b setObject:a forKey:[dict valueForKey:#"SubAssetClassName"]];
[mutdict setObject:b forKey:[dict valueForKey:#"AssetClassName"]];
}
}
}
Also, in most cases you should not be using valueForKey:. Use objectForKey: unless you have a clear and specific need to use key-value coding instead of simply getting an object from the dictionary for a given key.
I need to compare two array values one by one, if any value match with one another, i need to update the value and replace it from main array.
NSMutableArray is having an array of NSDictionary
NSArray is having an array of NSString objects.
My code is
NSMutableDictionary *dicOne = #{#"eveId":#"1",#"imgArray":#[#"one",#"two"]};
NSMutableDictionary *dicTwo = #{#"eveId":#"2",#"imgArray":#[#"Three",#"Four"]};
NSMutableArray *tempArray = [NSMutableArray array];
[tempArray addObject:dicOne];
[tempArray addObject:dicTwo];
NSArray *eventArray = [NSArray arrayWithObjects:#"1",#"2",#"3",nil];
NSString *fileName = #"Five";
for (int jj = 0; jj<[eventArray count]; jj++) {
NSString *topArrayEventId = [NSString stringWithFormat:#"%#",[eventArray objectAtIndex:jj]];
for (int kk = 0; kk<[tempArray count]; kk++) {
NSMutableDictionary *curentObj = [[tempArray objectAtIndex:kk]mutableCopy];
NSString *innerArrayEventId = [NSString stringWithFormat:#"%#",[curentObj valueForKey:#"eveId"]];
if ([innerArrayEventId isEqualToString:topArrayEventId]) {
NSLog(#"update inner array and replace main array");
NSMutableArray *imgArray = [NSMutableArray array];
imgArray = [[curentObj objectForKey:#"imgArray"]mutableCopy];
[imgArray addObject:fileName];
[curentObj setObject:[imgArray mutableCopy] forKey:#"imgArray"];
[tempArray replaceObjectAtIndex:kk withObject:[curentObj mutableCopy]];
}else{
NSLog(#"add new object to main array");
NSMutableDictionary* storeData = [NSMutableDictionary dictionary];
[storeData setObject:topArrayEventId forKey:#"eveId"];
NSMutableArray *imgArray = [NSMutableArray array];
[imgArray addObject:fileName];
[storeData setObject:imgArray forKey:#"imgArray"];
[tempArray addObject:storeData];
}
}
}
NSLog(#"final updation------->%#",tempArray);
I want output like below
(
{
eveId = 1;
imgArray = (
one,
two,
five
);
},
{
eveId = 2;
imgArray = (
Three,
Four,
five
);
}
{
eveId = 3;
imgArray = (
five
);
}
)
But i am not getting with that code..its giving wrong output. i having problem with inner loop.
Wrong output looks below
(
{
eveId = 1;
imgArray = (
one,
two,
Five
);
},
{
eveId = 2;
imgArray = (
Three,
Four,
Five
);
},
{
eveId = 1;
imgArray = (
Five,
Five
);
},
{
eveId = 2;
imgArray = (
Five,
Five
);
},
{
eveId = 2;
imgArray = (
Five,
Five
);
},
{
eveId = 3;
imgArray = (
Five,
Five
);
},
{
eveId = 3;
imgArray = (
Five,
Five
);
},
{
eveId = 3;
imgArray = (
Five,
Five
);
},
{
eveId = 3;
imgArray = (
Five,
Five
);
},
{
eveId = 3;
imgArray = (
Five,
Five
);
}
)
Predicate method using to achieve this
NSMutableDictionary *dicOne = #{#"eveId":#"1",#"imgArray":#[#"one",#"two"]};
NSMutableDictionary *dicTwo = #{#"eveId":#"2",#"imgArray":#[#"Three",#"Four"]};
NSMutableArray *tempArray = [NSMutableArray array];
[tempArray addObject:dicOne];
[tempArray addObject:dicTwo];
NSArray *eventArray = [NSArray arrayWithObjects:#"1",#"2",#"3",nil];
NSString *fileName = #"Five";
for (int jj = 0; jj<[eventArray count]; jj++) {
NSArray *resultArray = [tempArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"self.eveId = %#", [eventArray objectAtIndex:jj]]];
NSString *topArrayEventId = [NSString stringWithFormat:#"%#",[eventArray objectAtIndex:jj]];
if([resultArray count]>0) {
NSMutableDictionary *curentObj;
NSString *innerArrayEventId = [NSString stringWithFormat:#"%#",[[resultArray objectAtIndex:0] valueForKey:#"eveId"]];
if ([innerArrayEventId isEqualToString:topArrayEventId]) {
curentObj= [[resultArray objectAtIndex:0]mutableCopy];
NSMutableArray *imgArray = [NSMutableArray array];
imgArray = [[[resultArray objectAtIndex:0] objectForKey:#"imgArray"]mutableCopy];
[imgArray addObject:fileName];
[curentObj setObject:[imgArray mutableCopy] forKey:#"imgArray"];
[tempArray replaceObjectAtIndex:jj withObject:[curentObj mutableCopy]];
}
}
else {
NSMutableDictionary* storeData = [NSMutableDictionary dictionary];
[storeData setObject:topArrayEventId forKey:#"eveId"];
NSMutableArray *imgArray = [NSMutableArray array];
[imgArray addObject:fileName];
[storeData setObject:imgArray forKey:#"imgArray"];
[tempArray addObject:storeData];
}
}
NSLog(#"final updation------->%#",tempArray);
OutPut :
final updation------->(
{
eveId = 1;
imgArray = (
one,
two,
Five
);
},
{
eveId = 2;
imgArray = (
Three,
Four,
Five
);
},
{
eveId = 3;
imgArray = (
Five
);
}
)
Consider replacing NSMutableArray to use NSMutableSet
NSMutableArray *imgArray = [NSMutableArray array];
with
NSMutableSet *imgSet = [NSMutableSet set];
https://developer.apple.com/reference/foundation/nsmutableset
change this line
for (int kk = 0; kk<[tempArray count]; kk++)
with
for (int kk = jj; kk<[tempArray count]; kk++)
now run the code, is it working now?
in my project i applied the following code
NSDictionary *dict6 = [self cleanJsonToObject:responseData];
NSLog(#"str : %#",dict6);
diagnosisdict = [[[dict6 objectForKey:#"diagnoses"] objectAtIndex:0] objectForKey:#"DiagnosesHospitals"];
diagnosedictforname = [[[dict6 objectForKey:#"diagnoses"]objectAtIndex:0]objectForKey:#"Diagnoses"];
NSLog(#" for ref id =%# ,name of diagnose=%# data is= %#",refidstr,diagnosedictforname ,diagnosisdict);
and the output in console is comes out as in the form
str : {
diagnoses = (
{
Diagnoses = {
"diagnosis_name" = "TRANSIENT ISCHEMIA";
};
DiagnosesHospitals = {
"charge_amt" = "1300.00";
discharges = "11200.00";
"hospital_id" = 3341;
id = 163080;
"medicare_amt" = "100.00";
"total_amt" = "1100.00";
};
}
);
response = 200;
}
ref id =3341 ,name of diagnose={
"diagnosis_name" = "TRANSIENT ISCHEMIA";
} data is= {
"charge_amt" = "1300.00";
discharges = "11200.00";
"hospital_id" = 3341;
id = 163080;
"medicare_amt" = "100.00";
"total_amt" = "1100.00";
}
now i just want to embed the values of both the Dictionaries into one dictionary
someone please help me to sort out this issue.
Make a mutable copy of the first dictionary:
NSMutableDictionary * mutDic = [dic1 mutableCopy];
and then:
[mutDic addEntriesFromDictionary:dic2];
Try this code:
NSDictionary *dict6 = [self cleanJsonToObject:responseData];
NSLog(#"str : %#",dict6);
NSMutableDictionary *diagnosisdict = [[[dict6 objectForKey:#"diagnoses"] objectAtIndex:0] objectForKey:#"DiagnosesHospitals"];
NSDictionary *diagnosedictforname = [[[dict6 objectForKey:#"diagnoses"]objectAtIndex:0]objectForKey:#"Diagnoses"];
NSArray *keys = [diagnosedictforname allKeys];
for (int i =0; i < keys.count; i++) {
NSString *key = [keys objectAtIndex:i];
[diagnosisdict setValue:[diagnosedictforname valueForKey:key] forKey:key];
}
NSLog(#"your dic -> %#", diagnosisdict);
Hi I would like to know how to do when you click a button it will add a new object in the array and it will not affect the other objects like this:
here is my code:
x = x + 1;
dictData = [[NSMutableDictionary alloc] init];
for (int i = 0; i < x; i++) {
[dictData setObject:[menuData itemName] forKey:#"dishName"];
[dictData setObject:#"1" forKey:#"quantity"];
[dictData setObject:[menuData itemPrice] forKey:#"price"];
[arrMut addObject:dictData];
}
This is the current output
{
name = charles1;
},{
name = charles1;
}
This is the expected output
{
name = charles1;
},{
name = charles2;
}
because you are adding a same dictionary
change it to
for (int i = 0; i < x; i++) {
dictData = [[NSMutableDictionary alloc] init]; // move this line to inside of the loop
[dictData setObject:[menuData itemName] forKey:#"dishName"];
[dictData setObject:#"1" forKey:#"quantity"];
[dictData setObject:[menuData itemPrice] forKey:#"price"];
[arrMut addObject:dictData];
}
or
for (int i = 0; i < x; i++) {
[arrMut addObject:[#{#"dishName" : [menuData itemName],
#"quantity" : #"1",
#"price" : [menuData itemPrice],
} mutableCopy]];
}
The [menuData itemName] method seems to be returning the same object everytime.
You could insert menuData objects in an array and then access a menuData object on each iteration of the for loop.
NSMutableArray *arrMenuData = //This array should contain the menuData objects
for (int i = 0; i < x; i++) {
dictData = [[NSMutableDictionary alloc] init]; // move this line to inside of the loop
[dictData setObject:[[arrMenuData objectAtIndex:i] itemName] forKey:#"dishName"]; //A typecast to menuData type may be required, please test.
[dictData setObject:#"1" forKey:#"quantity"];
[dictData setObject:[[arrMenuData objectAtIndex:i] itemPrice] forKey:#"price"];
[arrMut addObject:dictData];
}
I would like to create an array for iOS platform like the PHP syntax below, many thanks ~~
$createArray = array ();
for ($i = 0; $i<10; $i++) {
$createArray[$i]['name'] = $name;
$createArray[$i]['age'] = $age;
}
Save your values in NSDictionary and add that dictionary into your array
NSMutableArray *theArray = [NSMutableArray array];
for (int indexValue = 0; indexValue<10; indexValue++) {
NSMutableDictionary *theDictionary = [[NSMutableDictionary alloc] init];
[theDictionary setObject:name forKey:#"name"];
[theDictionary setObject:age forKey:#"age"];
[theArray addObject:theDictionary]
}
While Retrieving time,
NSString *name = [[theArray objectAtIndex:indexValue] objectForKey:#"name"];
NSString *age = [[theArray objectAtIndex:indexValue] objectForKey:#"age"];
you might find it helpful:
array = [[NSMutableArray alloc] init];
for (int i = 0; i < 8; i++) {
NSMutableArray *subArray = [[NSMutableArray alloc] init];
for (int j = 0; j < 8; j++) {
[subArray addObject:[NSNumber numberWithInt:0]];
}
[array addObject:subArray];
[subArray release];
}
also check this question
Try this.
array = [[NSMutableArray alloc] init];
for (int i = 0; i < 10; i++) {
NSMutableArray *subArray = [[NSMutableArray alloc] init];
for (int j = 0; j < 2; j++) {
//Do your Stuff
// [subArray addObject:name];
// [subArray addObject:Age];
}
[array addObject:subArray];
}
OR
Why can't try with the NSDictionary
You can use this. But this is not better way in IOS.
NSMutableArray *array[20];
for (int i=0;i< 20; i++)
{
array[i] = [NSMutableArray array];
for (int j=0;j<3;j++)
{
NSMutableDictionary *theDictionary = [[NSMutableDictionary alloc] init];
[theDictionary setObject:name forKey:#"name"];
[theDictionary setObject:age forKey:#"age"];
[[array[i] addObject:theDictionary]
}
}
First you to have set An NSMutableDictionary on .h file
#interface MSRCommonLogic : NSObject
{
NSMutableDictionary *twoDimensionArray;
}
then have to use following functions in .m file
- (void)setValuesToArray :(int)rows cols:(int) col value:(id)value
{
if(!twoDimensionArray)
{
twoDimensionArray =[[NSMutableDictionary alloc]init];
}
NSString *strKey=[NSString stringWithFormat:#"%dVs%d",rows,col];
[twoDimensionArray setObject:value forKey:strKey];
}
- (id)getValueFromArray :(int)rows cols:(int) col
{
NSString *strKey=[NSString stringWithFormat:#"%dVs%d",rows,col];
return [twoDimensionArray valueForKey:strKey];
}