Parse.com Contains string or another string? - objective-c

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" ]];

Related

Parse query where key contains any object in array

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.

Objective-C: How to get data from selected row in table?

I need to retrieve a string from data from a selected row in a table to compare it to a string.
I call
NSArray *values = [self.tableArrayController selectedObjects];
To get the objects at the selected row. However, values only has a count of objects of 1, and everything seems to be one big, long string of all my values. I cannot search through the values because I won't always know what to search for. I need a specific value (of which they are organized by columns).
I was hoping that the array would be of a certain count and I could retrieve the string based on the index of the object for the string.
How should I get all the different values (organized by columns) for a selected row?
EDIT:
I populate a NSDictionary (withmore values than that, this is just an example). The string value is the column identifier. The object is the object I'm adding that I want to appear on the table. This part below already works as I want it to:
NSDictionary *dict =[NSDictionary dictionaryWithObjectsAndKeys:
object1, #"column",
object2, #"column2",
nil];
[self.tableArrayController addObject:dict];
Another way to get data from your table is to look at the "NSTableView" methods "selectedRow" & "selectedColumn".
With that information, you can get at the value you want directly via your array controller or you can use the NSTableViewDataSource method "tableView:objectValueForTableColumn:row:" to pass you back the NSString value of whatever it is that you're trying to get at.

How to remove only one instance of an object from an NSMutableArray?

The Apple documentation says that the - (void)removeObject:(id)anObject method removes all occurrences of the given object from an NSMutableArray.
Is there a way to remove only one occurrence of the object from the array?
If you have a particular instance that you want removed, which has a unique memory address but would otherwise compare equal to other instances, you would use removeObjectIdenticalTo:.
If you want to remove the first object in the array that fits the bill, use indexOfObject:, which finds the lowest index, followed by removeObjectAtIndex: You can also use indexesOfObjectsPassingTest: to get the list of all indexes that contain equal objects, as an NSIndexSet, and then pick one out from there -- perhaps lastIndex, e.g.
It is really simple:
[yourArray removeObjectAtIndex:[yourArray indexOfObject:yourObject]]
Yes, you want to find the index of the specific object you want to remove and call:
- (void)removeObjectAtIndex:(NSUInteger)index
See the Apple documentation for NSMutableArray here.

Is it possible to use KVO to obtain a sub-array of an array?

I have an array of objects which have an enum as one of their properties, I would like to obtain a filtered array based upon the value of the enum i.e. the returned array contains only objects which have a specified enum value.
I was wondering if KVO could be used as a tidy way of doing this, but haven't found anything suggesting it is?
You can do this by filtering the array using a predicate:
NSArray * filteredArray = [myArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"nameOfProperty == %d", theEnumValue]];
The string for the predicate names the property you're interested in, the value to which it should be compared, and the relationship the two must have for the predicate to evaluate as true.

Distinct Object using NSPredicate

I have an NSArray of custom objects. Consider that the custom objects have a PageNumber property. I would like to filter my NSArray with a condition like "customObject.PageNumber is distinct".
I know I can loop through the array and eliminate object with duplicate pageNumbers. But is there any easy way to do it? I have tried,
[myarray valueForKeyPath:#"distinctUnionOfObjects.pageNumber"];
It is giving me the unique page numbers (like 7, 8, 9). But I want the custom object itself rather than just page numbers. Can any predicate help me?
I have created a simple library, called Linq to ObjectiveC, which is a collection of methods that makes this kind of problem much easier to solve. In your case you need the Linq-to-ObjectiveC distinct method:
NSArray* itemsWithUniquePageNumbers = [items distinct:^id(id item) {
return [item pageNumber];
}];
This returns an array of objects, each one with a unique page number.
Yes, that is possible with the help of NSPredicate
customObject=[(NSArray*)[myArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"self.PageNumber==%d",pageNumber]] lastObject];
//pageNumber is an integer
The filtered array is an NSArray of your custom objects which is the result of filtering using the predicate. Since your page number is unique, it will return only an array of one object. We get that by passing lastObject message to it.
Refer:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Predicates/predicates.html#//apple_ref/doc/uid/TP40001798-SW1