How to extract data values and added to tableview - objective-c

How to extract the data from the below data structure, and display on tableview
id = extractData
[extractData objectForKey:#"data"] //BELOW IS OUTPUT:
( {
Name = {
id = 2;
name = rat;
};
Place = {
id = 7;
name = New York;
};
Item = {
Times = "100";
“expire” = 10-19-2015;
“value” = 300;
id = 1;
name = newname;
“checked” = 0;
};
Type = {
id = 1;
name = round;
};
},
{
Name = {
id = 2;
name = norway;
};
Place = {
id = 3;
name = ops;
};
Item = {
Times = "100";
“expire” = 10-18-2015;
“value” = 300;
id = 1;
name = new;
“checked” = 1;
};
Type = {
id = 2;
name = square;
};
}
)
Want to display Name,Place,Item,Type values in tableview cell with each iterations.
Can any one advice, how can extract value from above data.

Under your cellForRowAtIndexPath after initializing the custom cell add this code :
NSDictionary *dicVal = [yourDataArray objectAtIndex:indexPath.row];
cell.labelName.text = [[dicVal objectForKey:#"Name"] objectForKey:#"name"];
cell.labelPlace.text = [[dicVal objectForKey:#"Place"] objectForKey:#"name"];
cell.labelItem.text = [[dicVal objectForKey:#"Item"] objectForKey:#"name"];
cell.labelType.text = [[dicVal objectForKey:#"Type"] objectForKey:#"name"];
return yourCustomCell;
This should do the work. Thanks.

Related

How to extract NSString from NSDictionary that contains nested JSON

I'm trying to get NSStrings from an dictionary but the extracted strings contain the following
<__NSSingleObjectArrayI 0x28205fc00>(
Aaron
)
I expected s plain NSString.
Code:
NSString *name = [jsonDict valueForKeyPath:#"users.fName"];
NSLog(#"name, %#", name);
I though that using 'objectAtIndex' might work but it didn't.
Structure of NSDictionary (JSON) that I'm working with.
{
TYPE = UPDATE;
keys = (
{
calendarId = b306db7e1f924fdebade3813dd596f5d;
delFlag = 0;
editTime = "2019-10-23T12:02:32.0857328";
endTime = "2019-10-24T10:35:00";
insId = 20;
itemKeyId = 15;
keyId = ce71bc7ae3c145adad18a72e56cf0fab;
projectId = 950710ab2b96413cbfd186141e147b3e;
startTime = "2016-05-23T21:10:00";
updateStatus = 1;
validateCount = 6;
}
);
users = (
{
calendarId = 0;
delFlag = 0;
editTime = "2019-10-23T12:02:32.0794244";
fName = Aaron;
insId = 20;
itemUserId = 15;
keyId = ce71bc7ae3c145adad18a72e56cf0fab;
keyMac = 50338B11E912;
lName = Joines;
operId = 30222e0b4b0e4df6b669c3cf69245422;
projectId = 950710ab2b96413cbfd186141e147b3e;
userId = 8be21d1690bb46979a04b4e45a1ba625;
userPin = 123456;
}
);
}

Get 1 Segment of a Array

I want to get from my Array only on section.
This is my result...
(
{
foreName = FORENAME1;
gender = female;
id = 15;
key = 12345678901;
longName = LASTNAME1;
name = FORELAST1;
},
{
foreName = FORENAME2;
gender = male;
id = 52;
key = 12345678902;
longName = LASTNAME2;
name = FORELAST2;
},
{
foreName = FORENAME3;
gender = male;
id = 77;
key = 12345678903;
longName = LASTNAME3;
name = FORELAST3;
}
)
How can I search for id 77 and get the whole entry array for id 77 like the following example?
{
foreName = FORENAME3;
gender = male;
id = 77;
key = 12345678903;
longName = LASTNAME3;
name = FORELAST3;
}
That result is an array, and you want the element who's id==77...
- (NSDictionary *)elementInArray:(NSArray *)array withId:(NSNumber *)anId {
// there are a lot prettier ways, but this will be the clearest
for (NSDictionary *d in array) {
if ([d[#"id"] isEqual:anId]) return d;
}
return nil;
}
// call it like this
NSArray *array = // the array logged in the OP
NSDictionary result = [self elementInArray:array withId:#77];
NSLog(#"%#", result);

How to efficiently loop through multiple nested NSDictionaries and compare values?

I have the following structure in an NSDictionary that I got after parsing the XMl using XMl reader from here http://troybrant.net/blog/2010/09/simple-xml-to-nsdictionary-converter/:
{
Document = {
Page = (
{
TextLR = {
Line = {
LineProps = {
applyBreakingRules = true;
autoDecimalTabPos = 0;
breakJust = BreakOptimal;
direction = ES;
hyphenationZone = 0;
kindAlign = Left;
kindJust = FullInterWord;
left = 0;
presSuppressWiggle = true;
rightBreak = 0;
rightJustify = 0;
text = "\n\t\n\t\t\n\t\t\t\n\t\t\t\t";
treatHyphenAsRegular = true;
};
Text = {
text = "\n\t\t\t\tHello ";
};
betweenBottom = false;
betweenTop = false;
bottomEnable = false;
break = EndPara;
cpLim = 12;
cpStart = 0;
direction = ES;
doc = Main;
firstLineCp = true;
};
bottom = 114115;
cpLim = 12;
cpStart = 0;
doc = Main;
left = 0;
right = 2438349;
text = "\n\t\t";
top = 0;
};
cpLim = 81963072;
fBuggyJust = false;
fEmptyPage = false;
fHasBubbles = false;
fSlicedPage = false;
height = 3448422;
marginBottom = 3448422;
},
{
TextLR = {
Line = {
LineProps = {
applyBreakingRules = true;
autoDecimalTabPos = 0;
breakJust = BreakOptimal;
direction = ES;
hyphenationZone = 0;
kindAlign = Left;
kindJust = FullInterWord;
left = 0;
presSuppressWiggle = true;
rightBreak = 0;
rightJustify = 0;
text = "\n\t\n\t\t\n\t\t\t\n\t\t\t\t";
treatHyphenAsRegular = true;
};
Text = {
text = "\n\t\t\t\tHello SO ";
};
betweenBottom = false;
betweenTop = false;
bottomEnable = false;
break = EndPara;
cpLim = 12;
cpStart = 0;
direction = ES;
doc = Main;
firstLineCp = true;
};
bottom = 114115;
cpLim = 12;
cpStart = 0;
doc = Main;
left = 0;
right = 2438349;
text = "\n\t\t";
top = 0;
};
cpLim = 81963072;
fBuggyJust = false;
fEmptyPage = false;
fHasBubbles = false;
fSlicedPage = false;
height = 3448422;
marginBottom = 3448422;
}
);
doc = "simple1.htm";
xdpi = 72;
xmlns = "http://apple/sites;
"xmlns:xsi" = "http://www.w3.org/2001/XMLSchema-instance";
"xsi:schemaLocation" = "xmlns = "http://apple/sites/Dump.xsd";
ydpi = 72;
};
}
I have been struggling to iterate through this nested NSDictionary and extract each attribute to compare with another NSDictionary of similar structure. The dictionaries can be somewhat dynamic, as in there might be additional levels of nesting for different xml files but the dictionaries to compare are of exact similar structure with same tags. Is there a way to iterate and create nested dictionaries on the go and then have parallel loops going so that I can extract the values and compare with between 2 NSDictionaries? I have tried the following code, but I am stuck in finding a good way to make it create dictionaries dynamically at the same time compare values/attributes with another dictionary. Help is much appreciated.
NSArray *arrPages = [[_xmlDictionary_master objectForKey:#"Document"] objectForKey:#"Page"];//this would return the array of Page dictionaries
for(int i=0;i<[arrPages count];i++){
NSDictionary *aPage = [arrStation objectAtIndex:i];
NSLog(#"id = %#",[aStation objectForKey:#"id"]);
}
Above code returns 2 Nested key/value pairs which in turn have multiple nested dictionaries. I am finding it hard to know which value has nesting and which doesnt during run time.
First tip: Refactor your data so that this isn't such a difficult operation! If that's not possible there's a few things you can try.
Rather than a "for loop", use:
[arrStation enumerateObjectsWithOptions: NSEnumerationConcurrent
usingBlock: ^(NSDictionary *aPage, NSUInteger idx, BOOL *stop) {
// Code here for compare
}];
That may perform your operations in parallel.
Another technique is using GCD. Using a dispatch_async, then a dispatch_apply can achieve something very similar.
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_apply([arrStation count], dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t idx) {
// Work here
});
});
Spend some time reading through the Concurrency Programming Guide. It's a great way to understand what options are available and how they work.

NSOutLineView with nested NSArray

I have nested n level dynamic array, i want to bind it with NSOutlineView,
but i not getting how to bind it with NSOutlineView using its four delegate methods.
Nested Array like :
(
{
id = 3;
summary = "req 1";
"sub_requirement" =
(
{
id = 16;
"sub_requirement" = "";
summary = "sub req 1";
}
);
},
{
id = 5;
summary = "req 1-1";
"sub_requirement" =
(
{
id = 17;
"sub_requirement" = "";
summary = "sub req 1-1";
},
{
id = 21;
"sub_requirement" = "";
summary = "req 1";
}
);
},
{
id = 7;
summary = "req 1 1";
"sub_requirement" =
(
{
approved = 1;
budget = 3600;
description = "";
estimate = 3600;
"fk_feature" = 4;
"fk_parent" = 7;
"fk_requirementtype" = 1;
id = 18;
"sub_requirement" = "";
summary = "sub req 11";
}
);
},
{
id = 8;
"sub_requirement" = "";
summary = "req 1 2";
},
{
id = 12;
"sub_requirement" = "";
summary = "req 1";
},
{
id = 14;
"sub_requirement" = "";
summary = req1;
}
)
You could use an NSTreeController, some links and tutorials available here.

Accessing a Key in JSON Response

How in the following JSON response can I check if uids is nil ?
{
height = 480;
pid = "F#dd705e6b96a6e894556c0db84870501e_d97383cc80238c38b8907d99690fa1c9";
tags = (
{
attributes = {
glasses = {
confidence = 97;
value = false;
};
smiling = {
confidence = 97;
value = false;
};
};
center = {
x = "60.83";
y = "75.21";
};
gid = "<null>";
height = "49.58";
label = "";
manual = 0;
nose = {
x = "76.21";
y = "77.16";
};
pitch = "-5.55";
recognizable = 0;
roll = "-0.45";
uids = ( // <<<<<========== UIDs Key
);
width = "66.11";
yaw = "-19.56";
}
);
url = "http://face.com/images/ph/a6e529853432599dad6da996746cb070.jpg";
width = 360;
}
When I try :
if ([[[[photo objectForKey:#"tags"] objectAtIndex:0] valueForKey:#"uids"] count] == 0)
It keeps returning FALSE all the time.
I'm able to access a lot of these keys successfully. But it's just this one key that's giving me issues.
Like, to access the url key, I am using :
[photo objectForKey:#"url"];
And it's working absolutely fine.
What am I doing wrong with the uids key ?