data = (
{
date = "2016-01-20";
"end_time" = "11:10:00";
"function_code" = RCV;
"operator_id" = JOHN;
"start_time" = "11:00:00";
"total_time" = 10;
"total_units" = 19;
},
{
date = "2016-01-20";
"end_time" = "12:25:00";
"function_code" = PIK;
"operator_id" = JOHN;
"start_time" = "12:15:00";
"total_time" = 10;
"total_units" = 26;
}
)
this array containing 2 dictionary ,i have to get the endtime from the first dictionary and starttime from the second dictionary and i want to calculate the break time from this value.i know how to get the value dictionary . data[0][#"end_time"] and data[1][#"start_time"] this is sufficient if array contains two elements .but if array has more than 5 means,i want to iterate the array .i will update my code what i did ...
my array name is arrData
for (int i 0; i<arrData.count; i++) {
dictData =arrData[i];
NSString *startTime =[dictData objectForKey:#"start_time"];
NSString *totalTime = [dictData objectForKey:#"total_time"];
NSNumber *numTotalUnits =[dictData objectForKey:#"total_units"];
NSString *functionCode = [dictData objectForKey:#"function_code"];
NSString *endTime = [dictData objectForKey:#"end_time"];
NSLog(#"%#",endTime);
[array1 addObject:endTime];
if (arrData[i+1] > arrData.count) {
dictData = arrData[i+1];
NSLog(#"%#",dictData);
NSString *strStartTime = [dictData objectForKey:#"start_time"];
NSLog(#"%#",strStartTime);
[array1 addObject:strStartTime];
NSLog(#"%#",array1);
}
}
i tried this coding but i got error like index 2 beyond bounds[0..1]
Try for (int i = 0; i < arrData.count - 1; i++) {.
In your current code, you let i become equal to arrData.count - 1 in the loop, which means that when you check i + 1 you're looking past the final array element.
Related
I'm struggling with this dataset:
stopsArray: (
(
{
DistanceFromStart = 0;
Ordinal = 1;
Stop = {
Lat = "38.90282440185547";
Lon = "-77.03208160400391";
OrgAbbr = "<null>";
OrgId = DcCi;
StopCode = 1001259;
StopId = "DcCi_1445_7727";
StopName = "NW 14TH ST & NW K ST";
};
},
{
DistanceFromStart = "0.443314063224555";
Ordinal = 2;
Stop = {
Lat = "38.90834426879883";
Lon = "-77.03208923339844";
OrgAbbr = "<null>";
OrgId = DcCi;
StopCode = 1001393;
StopId = "DcCi_1445_7808";
StopName = "NW 14TH ST & NW RHODE ISLAND AV";
};
},
{
DistanceFromStart = "1.050716048463951";
Ordinal = 3;
Stop = {
Lat = "38.91703033447266";
Lon = "-77.03196716308594";
OrgAbbr = "<null>";
OrgId = DcCi;
StopCode = 17348;
StopId = "DcCi_1445_6674";
StopName = "NW 14TH ST & NW U ST";
};
},
I'm not sure if this is an array, a dictionary, or a combination. How do I tell the difference?
How would I pull out the "Ordinal" field, as well as elements in the "Stop" dataset?
To get the stopsArray data, I used this line:
stopsArray = [variantArray valueForKey:#"Stops"];
I've tried to pull the first Ordinal field (i.e. 1) with this:
NSMutableArray *array = stopsArray[0];
NSString *string = [array valueForKey:#"Ordinal"];
NSLog(#"string: %#", string);
But that results in:
string: (
1,
2,
3,
4,
5,
6,
7
)
Any ideas to stop my hair coming out would be most welcome.
I'll keep at it meanwhile.
Firstly, its an array of objects of type dictionary.
Secondly, the data doesn't seem to be properly formatted.
Its like an array with a single object of type dictionary, but that single object contains all the different dictionary values.
hence your result of
string: (
1,
2,
3,
4,
5,
6,
7
)
What seems to me the problem is the second open bracket
stopsArray: (
(
{
Try doing this...
NSMutableArray *array = stopsArray[0];
NSMutableArray *actualDataArray = array[0];
and then
NSString *string = [actualDataArray[INDEX_OF_DIC_OBJECT] valueForKey:#"Ordinal"];
NSLog(#"string: %#", string);
Lemme know if it works.
Updated Solution
What eventually worked for David DelMonte.
NSString *string = [[[stopsArray objectAtIndex:0] objectAtIndex:0] objectForKey:#"Ordinal"];
I know this question has been asked many times but not got the solution or i might not understood that's why posting the question.
for (int web=0; web<[web_arr count]; web++) {
AdditionalBookmark *web_book=[[AdditionalBookmark alloc]init];
UITextField *txtNam = (UITextField*)[self.view viewWithTag:1100+web];
NSString *txtName = txtNam.text;
UITextField *txtLin = (UITextField*)[self.view viewWithTag:1200+web];
NSString *txtLink = txtLin.text;
if ((txtName && txtName.length > 0)||(txtLink && txtLink.length > 0))
{
web_book.additionalBookmark_Name = txtName;
web_book.additionalBookmark_Link= txtLink;
web_book.contactDetails_Id=self.contactDetailsinfo.contactDetailsId;
web_book.additionalBookmark_Id=[[[web_arr objectAtIndex:web] valueForKey:#"WeblinkId"] intValue];
[dictWebLinks setObject:txtName forKey:#"WeblinkName"];
[dictWebLinks setObject:txtLink forKey:#"WeblinkUrl"];
[arrForWebLinks addObject:dictWebLinks];
}
[db updateIntoAdditionalBookmarkWithObject:web_book];
}
[dictUpdate setObject:arrForWebLinks forKey:#"Weblink"];
The problem am facing is this suppose there are two items in the array
Printing description of web_arr:
<__NSArrayM 0x170e4db60>(
{
WeblinkId = 9;
WeblinkName = Hdhd;
WeblinkUrl = "ie.comh";
},
{
WeblinkId = 10;
WeblinkName = Hd;
WeblinkUrl = "nd.com";
}
)
what i am getting is below:
Printing description of arrForWebLinks:
<__NSArrayM 0x170e4db30>(
{
WeblinkName = Hd;
WeblinkUrl = "nd.com";
},
{
WeblinkName = Hd;
WeblinkUrl = "nd.com";
}
)
dictionary is replacing the value for same key holding the latest value only, how can i mange to save the values in same format as in web_arr.
Any help is appreciable.
You are adding the same dictionary to the array over and over:
[dictWebLinks setObject:txtName forKey:#"WeblinkName"];
[dictWebLinks setObject:txtLink forKey:#"WeblinkUrl"];
[arrForWebLinks addObject:dictWebLinks];
You need to create a new one each time:
[arrForWebLinks addObject:#{
#"WeblinkName" : txtName,
#"WeblinkUrl" : txtLink
}];
(and remove dictWebLinks).
I'm trying to sort a JSON return into multiple sections according to each day. Here is the JSON return,
sales = (
{
"dis_dollars" = 0;
"dis_percent" = 0;
id = 111;
saleDay = 5;
saleMonth = 1;
saleTime = "20:02:39";
saleYear = 2014;
"total_price" = 25;
},
{
"dis_dollars" = 0;
"dis_percent" = 0;
id = 103;
saleDay = 2;
saleMonth = 1;
saleTime = "19:13:41";
saleYear = 2014;
"total_price" = 79;
},
{
"dis_dollars" = "0.55";
"dis_percent" = "0.10000000149012";
id = 101;
saleDay = 2;
saleMonth = 1;
saleTime = "10:41:11";
saleYear = 2014;
"total_price" = 22;
},
{
"dis_dollars" = 0;
"dis_percent" = 0;
id = 108;
saleDay = 1;
saleMonth = 1;
saleTime = "11:00:00";
saleYear = 2014;
"total_price" = 66;
}
);
}
I can get the number of sections if I do it this way
//...Connect to php script and get return json object
int saleDay = 0;
//Loop through json return
for (int i = 0;i < [sales count]; i++) {
NSDictionary* dict = [sales objectAtIndex:i];
Sale *eachSale =[[Sale alloc] initWithSaleId:[dict valueForKey:#"id"]
saleDay:[dict valueForKey:#"saleDay"]
saleMonth:[dict valueForKey:#"saleMonth"]
saleYear:[dict valueForKey:#"saleYear"]
saleTime:[dict valueForKey:#"saleTime"]
saleTotal:[dict valueForKey:#"total_price"]
saleDisDollars:[dict valueForKey:#"dis_dollars"]
saleDisPercent:[dict valueForKey:#"dis_percent"]];
NSLog(#"sale Day %#",eachSale.saleDay);
[_salesList addObject:eachSale];
int eachSaleDay = [eachSale.saleDay intValue];
// if the next day is different from the one before it, add a new section.
if (saleDay != eachSaleDay) {
_saleDaysCount += 1;
NSLog(#"eachSaleDay %i" ,eachSaleDay);
NSLog(#"salesDayCount %i" ,_saleDaysCount);
}
saleDay = eachSaleDay;
}
This will give me the correct number of sections, but how do I group the objects according to the day to find out the number of rows for each section, and what objects should go in each row?
I think I need to use NSSort Descriptors but I'm not sure where I would implement it and how to access the individual day arrays.
Any suggestions?
Thanks
I would simply add an additional field of type NSNumber with the complete date for each Sale while you are looping through your results.
eachSale.saleDateIdentifier = #(eachSale.saleYear.intValue * 10000 +
eachSale.saleMonth.intValue * 100 +
eachSale.saleDay.intValue);
Now it is easy to sort and count:
[_salesList sortUsingDescriptors:
#[[NSSortDescriptor sortDescriptorWithKey:#"saleDateIdentifier" ascending:NO]]];
NSArray *allDates = [_salesList valueForKeyPath:#"saleDateIdentifier"];
NSSet *uniqueDates = [NSSet setWithArray:allDates];
NSArray *sortedUniqueDates = [uniqueDates sortedArrayUsingDescriptors:
#[[NSSortDescriptor sortDescriptorWithKey:#"saleDateIdentifier" ascending:NO]]];
Your number of sections is sortedUniqueDates.count. For each section you could use the saleDateIdentifier to format the date string in titleForRowAtIndexPath.
BTW, maybe you do not want to use valueForKey in your init method for eachSale. Maybe you actually mean objectForKey. Better even, you should type the properties of the Sale class and use NSNumber and NSString as appropriate.
I am dealing a very basic problem, I have two dictionary keys I need to compare but when I do it doesn't give me correct answer, somehow I am failing to create the logic.
So I have this in a plist array of dictionaries and json array comming from server
I want to compare them if event exist in plist do certain thing if not do another thing;
PROBLEM:
According to my logic if two events in plist are equal to other two events comming from server if logic should be printed per for each dictionary coming from server , so in this case 2 times total , but in my code it is printed 4 times. basically it is satisfying the condition every single time.
NSLOG:
evreka find the id
evreka find the id
evreka find the id
evreka find the id
SOURCE:
in a plist;
createList (
{
"end_date" = "2013-07-24";
ends = "13:07:00";
"event_id" = 173;
"event_name" = Static;
location = Asdad;
"root_folder" = "dadapof#gmail.com";
"start_date" = "2013-07-24";
starts = "13:07:00";
"user_id" = 13;
},
{
"end_date" = "2013-07-25";
ends = "13:08:00";
"event_id" = 174;
"event_name" = "Event Delete";
location = Asdsad;
"root_folder" = "dadapof#gmail.com";
"start_date" = "2013-07-25";
starts = "13:08:00";
"user_id" = 13;
}
)
This comming from server
responseobj (
{
"end_date" = "2013-07-24";
ends = "13:07:00";
"event_id" = 173;
"event_name" = Static;
location = Asdad;
"root_folder" = "dadapof#gmail.com";
"start_date" = "2013-07-24";
starts = "13:07:00";
"user_id" = 13;
},
{
"end_date" = "2013-07-25";
ends = "13:08:00";
"event_id" = 174;
"event_name" = "Event Delete";
location = Asdsad;
"root_folder" = "dadapof#gmail.com";
"start_date" = "2013-07-25";
starts = "13:08:00";
"user_id" = 13;
}
)
CODE:
Comparison code;
if ([[NSFileManager defaultManager] fileExistsAtPath:dataPath]){
createList= [NSMutableArray arrayWithContentsOfFile:dataPath];
NSLog(#"createList %#",createList);
NSLog(#"responseobj %#",responseObj);
//compare plist and response object
for (int i=0; i<[responseObj count]; i++) {
NSDictionary *dataDict = [responseObj objectAtIndex:i];
NSString *event_name =[dataDict objectForKey:#"event_name"];
//NSString *event_id =[dataDict objectForKey:#"event_id"];
BOOL eventExistInPlist=NO;
for (int j=0; j<[createList count]; j++) {
NSDictionary *createDict = [createList objectAtIndex:i];
//NSString *create_id =[createDict objectForKey:#"event_id"];
NSNumber *create_id =[createDict objectForKey:#"event_id"];
if ([[dataDict objectForKey:#"event_id"] isEqual:[createDict objectForKey:#"event_id"]]) {
// if (create_id==event_id) {
NSLog(#"evreka find the id");
What I have tried:
I have tried to compare them as strings(isEqualtoString), numbers(==) and isEqual: method , they have all failed.
What am I doing wrong?
In
NSDictionary *createDict = [createList objectAtIndex:i];
^----- HERE
you are using the wrong index, it should be j instead of i for the inner loop.
Martin R's answer is where your issue is. I would change your logic though to include Objective-C fast enumeration instead of the standard for ++ loop. I also separated out the second for loop into a BOOL method for easier reading
for (NSDictionary *dataDict in responseObj) {
NSString *event_name =[dataDict objectForKey:#"event_name"];
if ([self matchFoundForEventId:event_name]) {
NSLog(#"Match found");
}
}
-(BOOL)matchFoundForEventId:(NSString *)eventId {
for (NSDictionary *dataDict in createList) {
NSString *createEvent = [dataDict objectForKey:#"event_id"];
if ([eventId isEqualToString:createEvent]) {
return YES;
}
}
return NO;
}
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.