Get 1 Segment of a Array - objective-c

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

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 the Array list based on the duplicate values in that Array Objective C

i have stuck up with below problem. I have got the below response.
(
{
CarrId = 102;
CarrName = "Fast and Furious";
CarrOptions = (
{
Id = 8;
Img = "400.jpg";
PImg = "412.jpg";
PQ = "S-30";
},
{
Id = 9;
Img = "400.jpg";
PImg = "412.jpg";
PQ = "M-30";
},
{
Id = 10;
Img = "603.jpg";
PImg = "611.jpg";
PQ = "S-30";
},
{
Id = 11;
Img = "603.jpg";
PImg = "611.jpg";
PQ = "M-30";
},
{
Id = 12;
Img = "603.jpg";
PImg = "611.jpg";
PQ = "L-30";
},
{
Id = 13;
Img = "738.jpg";
PImg = "749.jpg";
PQ = "S-30";
},
{
Id = 14;
Img = "738.jpg";
PImg = "749.jpg";
PQ = "M-30";
}
);
Status = 1;
}
)
Get the Array list based on the duplicate values in that Array Objective C. I need to get the array based on the PImg. For example if PImg = 412.jpg. i need the array something lik below
{
Id = 8;
Img = "400.jpg";
PImg = "412.jpg";
PQ = "S-30";
},
{
Id = 9;
Img = "400.jpg";
PImg = "412.jpg";
PQ = "M-30";
}
I have only done like getting the duplicates from the response
NSMutableDictionary * thisRow = [resultArray objectAtIndex:0];
NSMutableArray *pdctDtls = [thisRow objectForKey:#"CarrOptions"];
NSOrderedSet *orderedSet = [[NSOrderedSet orderedSetWithArray:pdctDtls] valueForKey:#"PImg"];
NSMutableArray *newarray = [orderedSet mutableCopy];
In newarray am getting the duplicate values. but if in the newarray if 412.jpg get the array related to 412.jpg. TIA
Try this. If you pass 611.jpg, you get 3 objects.
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF CONTAINS[c] %#", #"611.jpg"]; // AS PER YOUR REQUIREMENT
NSArray *array =[pdctDtls filteredArrayUsingPredicate:predicate];
You could use an NSPredicate like so:
NSArray *pdctDtls = thisRow[#"CarrOptions"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"PImg = %#", #"412.jpg"];
NSArray *resultsWithSamePimg = [pdctDtls filteredArrayUsingPredicate:predicate];
if (resultsWithSamePimg.count > 1) {
// we have some duplicates
// ...
}
But I would rather use a plain old for loop, which might be easier to debug and understand.

How to extract data values and added to tableview

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.

Predicating Nested Array

Doing a project on addressbook kind of app. I need to predicate address book contacts, result looks like this
(
{
AddressKey = (
);
Email = (
);
"JobTitle_Name" = "";
"Organisation_Name" = "";
Phone = (
{
phoneNumber = "+919502266633";
"phone_type" = home;
},
{
phoneNumber = 9703570333;
"phone_type" = work;
},
{
phoneNumber = 91234512345;
"phone_type" = iPhone;
},
{
phoneNumber = 91239123;
"phone_type" = mobile;
}
);
"first_name" = Raviraja;
imageKey = "";
"last_name" = "";
serialNumberKey = 53;
source = Device;
}
)
Need to predicate the array using phoneNumber key. I tried this one
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"ANY %K == %#",#"phoneNumber",[NSString stringWithFormat:#"%#",str]];
NSArray *array = [contactsArray filteredArrayUsingPredicate:predicate];
but not working eventhough phone number is matching.
Help me out guys.
The key path is wrong, it should be
[NSPredicate predicateWithFormat:#"ANY %K == %#",#"Phone.phoneNumber", ...]

Searching within subsubdictionaries of an array to return the parent dictionary (Objective C)

I have a structured array like the one below
(
{
id = 0;
name = "A";
tables = (
{
comment = "";
id = 0;
name = "T1";
},
{
comment = "";
id = 1;
name = "T2";
},
{
comment = "";
id = 4;
name = "T3";
}
);
},
{
id = 1;
name = "B";
tables = (
{
comment = "";
id = 5;
name = "T1";
},
{
comment = "";
id = 6;
name = "T2";
}
);
}
)
Given that I know the value of id key of one of the dictionaries (let's take id=6), how can I get its parent dictionary or to be more precise the value of name key (in this case B).
I've tried using predicate strings but the problem is that the parent dictionary has also a key named id, so I get the wrong results.
EDIT: id keys are unique (and it is also not necessary that ids of the elements in the first dictionary have lower intiger values than the ones in the dictionaries with higher indexes)
Try this, where array is your structured array
NSDictionary *parentDict=[[NSDictionary alloc]initWithDictionary:nil];
NSString *name=[[NSString alloc]init];
NSArray *table=[[NSArray alloc]initWithArray:nil];
for (NSDictionary * dict in array) {
table=[dict objectForKey:#"tables"];
for (NSDictionary *childDict in table) {
if ([[childDict objectForKey:#"id"]isEqualToString:#"6"]) {
//if your id value is int then use ==6 to check
parentDict=dict;
name=[dict objectForKey:#"name"];
return;
}
}
}