NSDictionary key comparison fails - ios

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

Related

How to Parse JSON Response and utilize it in Objective C?

I am developing an iOS app. I am getting JSON response, how do I Parse it? It is comprising of two different buttons data. How do I count it and show on buttons details?
JSON Response:
{
0 = {
date = "0000-00-00";
"is_active" = 1;
project = "New project";
"project_desc" = "New project";
"project_id" = 3;
time = "<null>";
"user_id" = 6;
};
1 = {
date = "2017-01-04";
"is_active" = 1;
project = "New Project 2";
"project_desc" = "New Project 2";
"project_id" = 4;
time = "06:29:54";
"user_id" = 6;
};
status = 1;
}
Parsing Code:
- (void)serverDidReceiveResponse:(NSDictionary *)response{
NSLog(#"%#",response);
for (NSDictionary *projectValue in response) {
_projectName = [projectValue valueForKey:#"time"];
_active = [projectValue valueForKey:#"is_active"];
}
for(NSDictionary*projectvalue in response)
{
for(NSDictionary *projectvalue1 in projectvalue)
{
_projectName = [projectValue1 valueForKey:#"time"]; _active = [projectValue1 valueForKey:#"is_active"];
}
}

How to Get Entire object from NSDictionary

This is the my data.
NSDictionary *dictResponse = [[NSDictionary alloc]init];
//Here is dictResponse value is
(
{
"created_on" = "0000-00-00 00:00:00";
id = 627;
"modified_on" = "0000-00-00 00:00:00";
name = "";
"user_id" = 99;
},
{
"created_on" = "2016-05-06 14:43:45";
id = 625;
"modified_on" = "2016-05-06 14:43:45";
name = Ggg;
"user_id" = 99;
},
{
"created_on" = "2016-05-03 17:21:52";
id = 623;
"modified_on" = "2016-05-03 17:21:52";
name = Qwerty;
"user_id" = 99;
},
{
"created_on" = "2016-04-29 20:12:38";
id = 601;
"modified_on" = "2016-04-29 20:12:38";
name = Teat2;
"user_id" = 99;
},
{
"created_on" = "2016-04-29 20:12:27";
id = 600;
"modified_on" = "2016-04-29 20:12:27";
name = Test1;
"user_id" = 99;
},
{
"created_on" = "2016-05-09 13:04:00";
id = 626;
"modified_on" = "2016-05-09 13:04:00";
name = Testios;
"user_id" = 99;
})
Now I want to access complete one set object i.e.
{
"created_on" = "2016-04-29 20:12:27";
id = 600;
"modified_on" = "2016-04-29 20:12:27";
name = Test1;
"user_id" = 99;
}
from my dictResponse
so when I use
dictResponse[0] or dictResponse1 .. am getting error, so How can I retrieve entire one set from NSDictionary ?
please check below console log output.
You can do like that :
id arrResponse = dictResponse;
if ([arrResponse isKindOfClass:[NSArray class]] && arrResponse != nil && arrResponse != (id)[NSNull null])
{
NSArray *arrResults = arrResponse;
//if you want retrieve specific entries then here is the code for that
for (int i = 0 ; i<arrResults.count; i++) {
NSString *strName = [[arrResults objectAtIndex:i]valueForKey:#"name"];
NSString *strCreated_on = [[arrResults objectAtIndex:i]valueForKey:#"created_on"];
NSString *strModified_on = [[arrResults objectAtIndex:i]valueForKey:#"modified_on"];
NSInteger userID = [[arrResults objectAtIndex:i]valueForKey:#"user_id"];
}
}
You can't access objects in a dictionary using an index (like dictResponse[0]). Objects in a dictionary don't have indexes, they have keys. To get an object from a dictionary you use the objectForKey method:
NSObject* myObject = [dictResponse objectForKey:#"my_key"];
You have not given enough detail in your question, so the following is a guess:
so when I use dictResponse[0] or dictResponse[1] .. am getting error
You never say what error you are getting. I'm guessing you get a compile error and not an execution one?
You seem to know your code is wrong as in the comments you write:
Yes I know this is not valid dictionary value but it is possible to get something like that value in NSDictionary, so question is not wrong, yes my response is array of dictionary. Means from console log I can give dictResponse[0] and it prints first part as expected.
Exactly, you can break the type system and store an array reference into a variable which is typed as holding a dictionary reference, but doing so is not a good idea!
When the compiler sees dictResponse[0] it recognises it syntactically as an array indexing operation, so it looks for a method on dictResponse which does array indexing. All it knows about dictResponse, and remember this is happening at compiler time, is that you have stated it is an NSDictionary *, so it looks for an array indexing method supported by NSDictionary and does not find one, giving the error:
Expected method to read an array element not found on object of type 'NSDictionary *'
Try correctly typing you variable and see if your code works.
HTH

Getting Value from Dictionary

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.

Parsing JSON Dictionary elements into an NSArray

I am trying to parse a JSON response into an NSArray for a specific item (geometry/location/lat). I easily manage to get the name into an array with
for (NSDictionary *object in results)
{
NSString *objectName = object[#"name"];
[MyArray addObject:objectName];
}
but I don't know how to dig down to the "lat" level in order to extract this...
JSON DATA full results: (
{
geometry = {
location = {
lat = “15.122494";
lng = “45.518496";
};
};
icon = "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business.png";
id = 1767ee03046d7ac7273be472310cfb6d7b2e6;
name = “Cafe Syracuse”;
photos = (
{
height = 768;
"html_attributions" = (
"From a Google User"
);
"photo_reference" = "CnRoAAAAlVZmfP0Dn3HIwcaEozcjYHqPMC_VlWcvz2sH0yYNjv9DS1GIccAvAp-LNaWxAWKvnEifhPU-1b8ETx7sXtOR7UKSs7tOBpai4FPM1y6M-lcL2qJ4M0dizbHStidxuGCF_QnE548qsE1tP2ZTdhjY_BIQKxSTS_-rPxGlOZmAG7oUUBSiV276H4CemarEXRozEtVr5Vs";
width = 1024;
}
);
"place_id" = ChIJB6K6CExRr8f4AvaA09U;
rating = "4.5";
reference = "CnRjAAAA5Yh4QrEnn7xH2URR7-gwwrhbUz2qmlwbCRW4-JkMfINdldXIYhQR8p2kTdiZiYsdX67nwINp963XIxysFPWOAVZ-okFCu0VH9NL66EWiPSVBFGSgYKXCd7LuhOFMgeU1yLuegyCMQ7RX6mjkhIQiVVok262SeOJGsChxoUXx6DKhp7t_uM51w2y9_bJAlscAA";
scope = GOOGLE;
types = (
cafe,
establishment
);
vicinity = “White glacier";
},
{
geometry = //Other item
Many thanks
This would help you to get lat and long:
NSString*lat= [[[object valueForKey:#"geometry"] valueForKey:#"location"] valueForKey:#"lat"];
NSString *lng= [[[object valueForKey:#"geometry"] valueForKey:#"location"] valueForKey:#"lng"];

get var from an array

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.

Resources