How to retrieve values for key 2 of a NSMutableDictionary into NSString?
My response data is given
response{ 0 = { email = "grover#gmail.com"; firstname = Grover; }; 1 = { email = "hems#gmail.com"; firstname = Gems; }; 2 = { email = "rohit#gmail.com"; firstname = Rohit; }; }
NSDictionary *key2 = response[#"2"];
NSString *email = key2[#"email"];
NSString *firstname = key2[#"firstname"];
Related
country = (
"3;India",
"4;US",
"5;UK",
)
state = (
"9;3;Uttar Pradesh",
"10;3;Maharashtra",
"11;3;Andaman and Nicobar Islands"
)
city = (
"110;3;10;pune",
"111;3;10;mumbai",
"112;3;10;nasik",
)
I want to show just the country name in dropdown list and want to pass it's Id to web service. If user selects India, I have to pass 3. How can I do this?
As I understand you have an array with strings in it.
If so, you just need to split your strings.
objective-c code
NSArray *country = #[#"3;India", #"4;US", #"5;UK"];
NSString *countryStr = country[0]; // 3; India
NSArray *countryArr = [countryStr componentsSeparatedByString:#";"];
NSString *countryCode = countryArr.firstObject; //3
NSString *countryName = countryArr.lastObject; //India
NSLog(#"%#", countryCode); //3
NSLog(#"%#", countryName); //India
swift code
let country : NSArray = ["3;India", "4;US", "5;UK"]
let countryStr = country[0]
let countryArr : NSArray = countryStr.componentsSeparatedByString(";")
let countryCode = countryArr.firstObject
let countryName = countryArr.lastObject
if let countryCodePr = countryCode {
print(countryCodePr)
}
if let countryNamePr = countryName {
print(countryNamePr)
}
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.
<__NSArrayM 0x82701fe0>(
{
Distance = "2.08";
address = Manjalpur;
dob = "09-04-1986 ";
email = "kan#t.com";
facebookid = "";
fname = kanan;
gender = Female;
imageurl = "https://foursquare.com/img/categories_v2/food/indian_";
lastlogin = "/Date(1378288646803+0000)/";
latitude = "22.27052139756";
lname = kanan;
longitude = "73.196614980698";
name = "Green Chillies";
photopath1 = "";
time = "/Date(1378288744627+0000)/";
userid = 202;
},
{
Distance = "2.08";
address = Manjalpur;
dob = "09-04-1986 ";
email = "kan#t.com";
facebookid = "";
fname = kanan;
gender = Female;
imageurl = "https://ss1.4sqi.net/img/categories_v2/food/indian_";
lastlogin = "/Date(1378288646803+0000)/";
latitude = "22.27052139756";
lname = kanan;
longitude = "73.196614980698";
name = "Green Chillies";
photopath1 = "";
time = "/Date(1378288744627+0000)/";
userid = 202;
},
{
Distance = "2.12";
address = Manjalpur;
dob = "03-11-1983";
email = "r#tax.com";
facebookid = 100001835956323;
fname = Rohan;
gender = Female;
imageurl = "https://foursquare.com/img/categories_v2/building/apartment_";
lastlogin = "/Date(1385115357020+0000)/";
latitude = "22.270002988846";
lname = Panchal;
longitude = "73.197391863176";
name = "Tulsidham circle";
photopath1 = "http://taxsmart.in/FTP_Data/S55_2.jpg";
time = "/Date(1381412082083+0000)/";
userid = 55;
},
{
Distance = "2.12";
address = Manjalpur;
dob = "03-11-1983";
email = "r#tax.com";
facebookid = 100001835956323;
fname = Rohan;
gender = Female;
imageurl = "https://ss1.4sqi.net/img/categories_v2/building/apartment_";
lastlogin = "/Date(1385115357020+0000)/";
latitude = "22.270002988846";
lname = Panchal;
longitude = "73.197391863176";
name = "Tulsidham circle";
photopath1 = "http://taxsmart.in/FTP_Data/S55_2.jpg";
time = "/Date(1381412082083+0000)/";
userid = 55;
}
)
my NSDictionary *finalResult print this result
Actually there are only two records but from web service I am getting
this type of result they cannot distinct because of unique imageurl.
so how can i remove duplicate data set.The same user data are only differ from each other with key imageurl.
I just want only single record of user does not matter which one(any one). So finally I will get only two record
//You can filter you response like this:
//tempArray is data array which content webservice response
NSMutableArray *mainArray = [[NSMutableArray alloc]init];
for (int i = 0; i< [temprray count]; i ++) {
if ([mainArray count]==0) {
[mainArray addObject:[temprray objectAtIndex:0]];
}
else {
NSDictionary *tempDict = [temprray objectAtIndex:i];
BOOL isRecordFound = NO;
for (int j = 0; j < [mainArray count]; j++) {
NSDictionary *tempinnerDict = [mainArray objectAtIndex:j];
if ([[tempinnerDict valueForKey:#"userid"] integerValue] == [[tempDict valueForKey:#"userid"] integerValue]) {
isRecordFound = YES;
break;
}
}
if (!isRecordFound) {
[mainArray addObject:tempDict];
}
}
}
It may be a webservice error. Best way is to contact web team or you can remove the duplicates by using below way
You can check the duplicate dictionary with the below method
- (BOOL)isEqualToDictionary:(NSDictionary *)otherDictionary
Inside a for loop you can check the duplicates and if there anyone, you can remove it from an NSMutableArray using
- (void)removeObjectAtIndex:(NSUInteger)index
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"]);
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.