Parse query where key contains any object in array - objective-c

My parse class has a property called "postcodes", which is an array of numbers.
In my app, I have an array of "relevantPostcodes". I want to create a query where the postcodes key contains any object from the "relevantPostcodes" array.
Something like [query whereKey:#"postcodes" containsAnyObjectInArray:relevantPostcodes"]. Any ideas?

You can use [query whereKey:#"postcodes" containedIn:relevantPostcodes]. See the description in the doc. Just like the equal to methods, I think it will operate on array type attributes and do what you need.

Related

Xcode: Parse.com query that requires a particular key's array contains every AND ONLY the elements of the provided array

In parse.com I have a Messages class and the objects in that class have a column named "recipientIds". The recipientIds key contains an array of users for each object. I want to query only the objects whose recipientIds array contain the exact same array items as the provided array (no more, no less)
I tried both of the following
[query whereKey:#"recipientIds" containedIn:myFriendsArray];
[query whereKey:#"recipientIds" containsAllObjectsInArray:myFriendsArray];
but these didn't work because they both returned additional objects that contained additional recipients. Is there a different way to arrange my query so that I only receive the objects with the exact same recipients as myFriendsArray. I basically need a method that containsAllAndOnlyObjectsInArray.

Parse.com Contains string or another string?

I have a array in my Parse cloud for example ["Peter","Steve"] and i want to query this object, but only have one of these two strings (for example #"Peter"). Is there any opportunity to query that?
Thanks,
Beeke
Yes. The whereKey:equalTo: method, when applied to an array column checks for containment in the array, so...
[query whereKey:#"firstNameArray" equalTo:#"Peter"];
... will check if #"Peter" is in the object's firstNameArray column.
EDIT - to check whether the array contains any in a set of elements, you can use whereKey:containedIn: ...
[query whereKey:#"firstNameArray" containedIn:#[ #"Peter", #"Paul" ]];

Using KVC to get a list of objects' properties

In this SO post, the chosen answer uses the syntax below to get a list of frequentFlyerNumbers from a list of passengers this way.
NSArray *frequentFlyerNumbers =
someFlight.passengers.frequentFlyerNumbers;
How is this possible given that passengers is an array and compiler can't infer the type that goes into the array?
I got the following error when implementing passengers.
Property 'frequentFlyerNumbers' not found on object of type
'NSMutableArray *'
The answer you cite is poorly written. The code as presented there won't work.
Dot-syntax, in plain code, is just shorthand for an accessor: someFlight.passengers means [someFlight passengers], which from the context appears to return a NSMutableArray. NSMutableArray of course doesn't have a frequentFlyerNumbers property itself.
To get the effect you want, do something like this:
NSArray *frequentFlyerNumbers = [someFlight valueForKeyPath:#"passengers.frequentFlyerNumbers"];
This will work, and return an array of frequentFlyerNumbers. When there's an array or set along a key path, it "projects" the subsequent path across all members of that array or set, producing an array or set of the results.

NSDictionary with same object key

i have a NSDictionary with arrays
A1: a,b,c,d,e,f and
A2:1,1,1,2,2,3
i want to get the objects of key 1
i used:
[dicitonay objectsForKey: #"1"];
but i am get only 1 object, how to get all objects?
If your dictionary contains two arrays, when you do 'objectForKey', one of the arrays(a single array object) will be returned. Without looking at your actual code, you should now have a single array object. You can use the typical array methods to access the objects inside the array.

Creating an array from properties of objects in another array

Is there any convenient way to take an array/set of objects and create a new array/set containing some property of each item in the first array?
For example, an array contains Car objects. I need an array of licensePlates, where each car has an NSObject car.licensePlate.
Currently I just iterate through the first array adding objects to my mutable results array, but was wondering if there is an instantiation method that exists for this (checked the docs for NSArray).
This will return an array containing the value of licensePlate from each item in the myCars array:
NSArray *licensePlates = [myCars valueForKeyPath:#"licensePlate"]
If you want only unique items (for example), you can do something like this:
NSArray *licensePlates = [myCars valueForKeyPath:#"#distinctUnionOfObjects.licensePlate"];
For more possibilities, see the Collection Operators documentation in the Key-Value Coding Programming Guide.