Predicating Nested Array - objective-c

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", ...]

Related

How get a key value in NSMutableArray?

A NSMutableArray called arrayDB, and it gets data from server by using AFNetworking, like this
[self.arrayDB addObjectsFromArray:[responseObject objectForKey:#"list"]];
NSLog(#"print:%#",self.arrayDB);
And the result like this.
print:(
{
id = 10;
key = 7707eca6ea4d2db923c8b7b4e6ec094c;
},
{
id = 9;
key = 7aaf962bc4df61a3b44c544c58fc6c82;
},
{
id = 7;
key = ce9d9e99a96d6417c6b34321c820c4c0;
},
{
id = 6;
key = 6086663465e5813d08f862eb443e2623;
},
{
id = 4;
key = 5d9519dd7de26f0139d6028fb83a4ead;
}
)
And now, I use sql statement to describe my needs, select key from arrayDB where id=10, how can I write in objective-C?
You can use NSPredicate like this:
NSArray *myArray = [self.arrayDB filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"(id == %#)", #"10"]];

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.

NSPredicate query for a Realm object

Could someone please help me construct a realm query to search for a video(s) of a particular entryid value, for extra points a second query that would find both videos by their entryids ?
Thank you!
Video {
nid = 1;
video_title = OK;
video_description = Description {
value = Description;
};
video_kaltura = Kaltura {
entryid = 0_abababa3;
mediatype = 1;
};
}
Video {
nid = 2;
video_title = OK;
video_description = Description {
value = Description;
};
video_kaltura = Kaltura {
entryid = 0_cdcdcdc3;
mediatype = 1;
};
}
I've tried things like:
NSPredicate *pred = [NSPredicate predicateWithFormat:#"video_kaltura['entryid'] CONTAINS[c] '0_abababa3'"];
RLMResults *test = [Video objectsWithPredicate:pred];
and their variations to no avail.
NSPredicate uses key paths to traverse properties (object.someProperty.otherProperty).
Based on your example data, you'd want a predicate like video_kalutra.entryid == '0_abababa3'.

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

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