I have an array (NSArray) with the following content:
dates {
dateOne = {
"date_one" = "2011-11-30";
name = "test1";
dateOne=true
};
dateTwo = {
"date_two" = "2011-11-30";
name = "test2";
dateTwo=false
};
dateThree = {
"date_Three" = "2011-11-30";
dateTwo=true;
name = "test3";
};
the content above is being generating with the following line of code:
NSArray *dates = [myXmlParsed valueForKeyPath:#"myXml.dates"];
using NSLog:
NSLog(#"%#, '%#'", NSStringFromClass([dates class]), dates);
I get the following output:
__NSDictionaryM, '{
dateOne = {
"date_one" = "2011-11-30";
name = "test1";
dateOne=true
};
dateTwo = {
"date_two" = "2011-11-30";
name = "test2";
dateTwo=false
name = "test3";
};
dateThree = {
"date_Three" = "2011-11-30";
name = "test1";
dateTwo=true;
name = "test3";
};
}'
but I'm having problems getting the values of dictionaries inside of the array. My question is how can get the data of dateThree and how can I access to individual node of information for example date_Three ?
I'll really appreciate your help guys
I figure it out. I was using nsarray when I should have done nsmutabledictionary.
NSMutableDictionary *dates=[[NSMutableDictionary alloc] initWithDictionary: [resultDict valueForKeyPath:#"myXml.dates"]];
Now I can query any of the values:
NSLog(#"value %#", [[dates valueForKey:#"dateOne"] valueForKey:#"date_one"]);
Related
I have an array of dictionaries with N keys. I want to create an array of unique dicts which contains only some keys.
Example. My dict:
{
"m_anno" = 2017;
"m_c_ultimo" = "4.130";
"m_cod" = 4522;
"m_cod_art" = "*B";
"m_des" = "SCRITTA BIANCA NISSAN";
"m_ditta" = SIS;
"m_prz" = "0.000";
"m_qta" = "1.000";
"m_sconto" = {
};
},
{
"m_anno" = 2017;
"m_c_ultimo" = "25.020";
"m_cod" = 4522;
"m_cod_art" = "000/01200";
"m_des" = "CABLAGGIO X TIMONE";
"m_ditta" = SIS;
"m_prz" = "0.000";
"m_qta" = "1.000";
"m_sconto" = {
};
},
{
"m_anno" = 2017;
"m_c_ultimo" = "1.000";
"m_cod" = 4523;
"m_cod_art" = 000000;
"m_des" = "BLISTER O-RING 3106";
"m_ditta" = SIS;
"m_prz" = "0.000";
"m_qta" = "1.000";
"m_sconto" = {
};
},
I want to get unique values of m_anno and m_cod keys. Expected result:
{
{
"m_anno" = 2017
"m_cod" = 4522
}
{
"m_anno" = 2017
"m_cod" = 4523
}
}
Which is the simplest way?
Basically you have to iterate over the array (quelle surprise!) and put it in a collection, if the pair doesn't exist yet. The second task can be accomplished by using an instance of NSSet or NSOrderedSet depending on the requirement that the order has to be preserved:
NSArray *values = …; // You start with this
NSMutableOrderedSet *uniquePairs = [NSMutableOrderedSet new];
for( NSDictionary *value in values )
{
NSDictionary *pair = #{ #"m_anno":[value objectForKey:#"m_anno"], #"m_cod":[value objectForKey:#"m_cod"] }; // Create smaller version
[uniquePairs addObject:pair];
}
There might be a more elegant way. (Depends of the point of view)
Create an addition to dictionaries, that reduces the array to the desired keys:
NSDictionary( UnifyAnnoAndCodeAddition )
- (NSDictionary*)annoAndCod
{
return #{ #"m_anno":[self objectForkey:#"m_anno"], #"m_cod":[self objectForKey:#"m_code"]};
}
Then use key-value coding to create an array of the reduced dictionaries and make a set of this:
NSArray *values = …; // You start with this
NSArray *pairs = [values valueForKey:#"annoAndCod"];
NSOrderedSet = [NSOrderedSet orderedSetWithArray:pairs];
Well, at least it is shorter.
Unable to parse the format string "json.option_titles.ANY.options.ANY.jobID == %#
Although i can use the for loop but i might have option titles length long so it will be not good. will it be possible using nspredicate?
This is my sample Data
(
{
dept = 1194;
id = 2299;
job = 22048;
json = {
name = tester;
"option_titles" = (
{
"custom_option_title" = "";
"financing_option" = "";
options = (
{
hours = "2.00";
jobID = 22048;
optionDesc = "Sample Description";
},
{
hours = "2.00";
jobID = 22048;
optionDesc = "Sample Description";
},
);
}
);
"system_observations" = "";
};
name = tester;
"sort_order" = 178;
"system_observations" = "";
},
{
dept = 1195;
id = 2300;
job = 22000;
json = {
name = tester second;
"option_titles" = (
{
"custom_option_title" = "title option";
"financing_option" = "";
options = (
{
hours = "2.00";
jobID = 22000;
optionDesc = "Sample Description";
},
{
hours = "2.00";
jobID = 22000;
optionDesc = "Sample Description";
},
);
}
);
"system_observations" = "";
};
name = tester;
"sort_order" = 179;
"system_observations" = "";
}
)
Tried Code:-
NSArray *allJobs = [self getAllJobs];
allJobs = [allJobs filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"(dept_id == %#)",deptId]];
NSMutableArray *coumpoArray = [NSMutableArray new];
for (NSDictionary *job in allJobs) {
NSPredicate *predicateDeepFilterJobID = [NSPredicate predicateWithFormat:#"json.option_titles.options.jobID == %#",job[#"id" ]];
[coumpoArray addObject:predicateDeepFilterJobID];
}
NSPredicate *pr = [NSCompoundPredicate orPredicateWithSubpredicates:coumpoArray];
filterBydept = [arrayTemplate filteredArrayUsingPredicate:pr];
Tried another predicate:-
NSArray *allJobs = [self getAllJobs];
allJobs = [allJobs filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"(dept_id == %#)",deptId]];
NSMutableArray *coumpoArray = [NSMutableArray new];
for (NSDictionary *job in allJobs) {
NSPredicate *predicateDeepFilterJobID = [NSPredicate predicateWithFormat:#"json.option_titles.ANY.options.ANY.jobID == %#",job[#"id" ]];
[coumpoArray addObject:predicateDeepFilterJobID];
}
NSPredicate *pr = [NSCompoundPredicate orPredicateWithSubpredicates:coumpoArray];
filterBydept = [arrayTemplate filteredArrayUsingPredicate:pr];
Using that code I'm able to get all the list of array.
Looking a lot of tutorial but I'm not able to get the var called "id" if the "name" isEqual to "Timeline Photos".
Code:
NSArray *feed =[result objectForKey:#"data"];
for (NSDictionary *dict in feed) {
NSLog(#"%#",dict);
}
Result (Example of the first two result):
2015-02-03 13:59:21.246 Project[708:29363] {
"can_upload" = 0;
count = 68;
"cover_photo" = 10200808394440000;
"created_time" = "2011-03-09T17:25:03+0000";
from = {
id = 10204248513160000;
name = "XX XX";
};
id = 1716972770000;
link = "https://www.facebook.com/album.php?..";
name = "Mobile Uploads";
privacy = everyone;
type = mobile;
"updated_time" = "2015-01-31T13:28:32+0000";
}
2015-02-03 13:59:21.247 Project[708:29363] {
"can_upload" = 0;
count = 11;
"cover_photo" = 4383404270000;
"created_time" = "2010-02-15T15:50:30+0000";
from = {
id = 10204248513160000;
name = "XX XX";
};
id = 1267183610000;
link = "https://www.facebook.com/album.php?...";
name = "Timeline Photos";
privacy = everyone;
type = wall;
"updated_time" = "2015-01-28T18:26:52+0000";
}
Try this:
NSArray *feed =[result objectForKey:#"data"];
for (NSDictionary *dict in feed) {
if ([dict[#"name"] isEqualToString:#"Timeline Photos"])
NSLog(#"%#",dict[#"id"]);
}
This will only print id when name is equal to Timeline Photos.
Actually, you are not working with an array, but with a dictionary.
Hope this helps.
I have a NSString =#"KEY3"; and a NSMutableDictionary *AllValues ; like the below . I just need to check whether the string is one of the element of the AllValues Dictionary
KEY1 {
MLCAvailable = "test";
MLCColorCode = "test2";
NotesCount = "test3";
},
KEY2 {
MLCAvailable = "test";
MLCColorCode = "test2";
NotesCount = "test3";
},
KEY3 {
MLCAvailable = "test";
MLCColorCode = "test2";
NotesCount = "test3";
},
KEY4 {
MLCAvailable = "test";
MLCColorCode = "test2";
NotesCount = "test3";
},
How should i find whether they KEY 3 is present in the given MutableDictionary ?
You can just try and get the element...
if(AllValues[#"KEY3"]) {
//The element exists
}
You can find it with the help of for loop
for (NSString *strKey in [AllValues allKeys]) {
if ([strKey isEqualToString:#"KEY3"]) {
//your condition
}
}
Is it Helpfull?
i want to retrive KpiName key(KpiData is array inside kpiName is dictionary key) and my data structure mentioned below
--Array of dictionaries inside array of dictinary
ex..listOfProjectsArray--(
{
alertSeverity = 1;
customerName = TPMG;
endDate = "05-05-2013";
isProjectHide = 1;
kpiData = (
{
KpiName = "Change Request ";
kpiActual = 14;
kpiPlanned = "";
kpiUnit = "";
},
{
KpiName = "Aged Debit";
kpiActual = 24000;
kpiPlanned = "";
kpiUnit = EUR;
},
);
lastInvoiceDate = "05-04-2013";
nextBillingDate = "05-04-2013";
nextMilestoneDate = "10-04-2013";
nextReviewDate = "08-04-2013";
plannedCompletionDate = "04-05-2013";
projectID = 3000;
projectName = "Rain Harvesting";
projectSortType = "";
projectStatus = 1;
startDate = "01-01-2013";
},
i tried this one but its returning null
NSString *kpiName = [[[[listOfProjectsArray objectAtIndex:indexPath.row] valueForKey:#"kpiData"] objectAtIndex:0]valueForKey:#"kpiName"];
NSLog(#"kpiname:%#", kpiName);
Try this
NSString *kpiName =[[[[listOfProjectsArray objectAtIndex:indexPath.row] objectForKey:#"kpiData"]objectAtIndex:0]objectForKey:#"KpiName"];
I was mentioned wrong dictionary key
NSString *kpiName = [[[[listOfProjectsArray objectAtIndex:indexPath.row] valueForKey:#"kpiData"] objectAtIndex:0]valueForKey:#"kpiName"];
NSLog(#"kpiname:%#", kpiName);
correct one is KpiName key instead of kpiName.