How to Find whether the given string is present in the NSMutableDictionary - ios

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?

Related

How to get 'key' from 'value' in dictionary which is inside an array?

Here is dictionary which in inside an array,
array With Dict : (
{
id = 1;
name = Arjun;
},
{
id = 2;
name = Karan;
},
{
id = 3;
name = Ajit;
},
{
id = 4;
name = Prashant;
},
{
id = 5;
name = Sushant;
}
)
When I will select any 'value', I want to fetch the 'key' associated with that value.
for example :
Suppose I selected 'Prashant' and I want its 'id' i.e 4.
How to get 'key' from 'value'?
NSString *myName = #"Prashant";
for (NSDictionary *dict in array) {
if ([dict[#"name"] isEqualToString:myName]) {
NSLog(#"%#", dict[#"id"]);
break;
}
}
for (NSDictionary*dic in array)
{
NSString * name = dic[#"name"];
if([name isEqual:currentSelectedName])
{
NSString * id = dic[#"id"];
NSLog(#"id is : %#",id);
}
}
You can loop thru the Array and find the matching entry - value you are looking for. Then get its key.

I am filtering a multilevel NSArray using NSPredicate., i am trying to filter 3rd child of array using predicate but i am getting this

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

How to create a sub NSArray that includes key-value pairs?

I have the following NsArray and I want to sort it and create a sub array that include specific key-value pairs.
{
scores = (
{
category = 2;
"league_code" = epl;
"team_away" = Everton;
"team_home" = Liverpool;
},
{
category = 2;
"league_code" = epl;
"team_away" = Fulham;
"team_home" = "Swansea City";
},
{
category = 3;
"league_code" = ita;
"team_away" = Torino;
"team_home" = Milan;
},
{
category = 3;
"league_code" = ita;
"team_away" = Lazio;
"team_home" = Juve;
}
}
}
and I would like to create a new array or just modify array to include only objects with "league_code" = epl key.
So I want to get
{
scores = (
{
category = 2;
"league_code" = epl;
"team_away" = Everton;
"team_home" = Liverpool;
},
{
category = 2;
"league_code" = epl;
"team_away" = Fulham;
"team_home" = "Swansea City";
}
}
}
I tried to convert it to use dictionaryWithObjects:forKeys but it didn't work.
NSMutableArray* newArray = [NSMutableArray array];
NSArray* oldArray = outerDictionary[#"scores"];
for (NSDictionary* dictEntry in oldArray) {
NSString* leagueCode = dictEntry[#"league_code"];
if ([leagueCode isEqualToString #"epl"]) {
[newArray addObject:dictEntry];
}
}
Was that all that hard?
Loop Through and the if it matches your conditions add it to a dictionary. I recommend the for-loop but any loop while do.

How to extract elements in NSArray using NSPredicate

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

iphone sdk+How to retrive dictionary inside array of dictionary values

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.

Resources