• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: filteredArrayUsingPredicate and points
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: filteredArrayUsingPredicate and points


  • Subject: Re: filteredArrayUsingPredicate and points
  • From: Jim Turner <email@hidden>
  • Date: Wed, 15 Apr 2009 13:07:00 -0500

On Wed, Apr 15, 2009 at 9:05 AM, Alexey Baev <email@hidden> wrote:
> Hi.
>
> I have class MyDot. This class has function:
> - (BOOL) containsPoint: (NSPoint) pt;
>
> I have also NSArray, which contains several MyDot objects:
> NSArray* dots;
>
> I want to get new array
> NSArray* newDots
> which contains MyDot objects and
> for these dots containsPoint:(NSPoint)pt = YES.
>
> How can I do that?
>
> I would like to write something like that:
>
> NSPoint checkedPoint = NSMakePoint(100, 100);
>
> NSPredicate* predicate = [NSPredicate predicateWithFormat: @"containsPoint
> %p == YES ", checkedPoint]; //i don't know what to write here
>
> NSArray* newDots = [dots filteredArrayUsingPredicate: predicate];
>
> I would be glad if you helped me.
>
> --
> Alexey Baev

Alexey,

It's quite possible, although it takes a bit more that a simple
predicateWithFormat: can do.  You'll need to use NSExpression's
expressionForFunction:selectorName:arguments: along with a
NSComparisonPredicate.  Your MyDot class will also need a method
that's able to accept a NSValue object (containing a NSPoint) and
return an NSNumber.  Something like...

-(NSNumber *) containsThisPoint: (NSValue *) aPoint;

So to filter your array:

NSArray *myArrayOfObjects = ...
NSPoint tgtPoint = ...

NSExpression *functionExp = [NSExpression
expressionForFunction:[NSExpression expressionForEvaluatedObject]
selectorName:@"containsThisPoint:" arguments:[NSArray
arrayWithObject:[NSExpression expressionForConstantValue:[NSValue
valueWithPoint:tgtPoint]]]];

NSPredicate *searchPredicate = [NSComparisonPredicate
predicateWithLeftExpression:functionExp rightExpression:[NSExpression
expressionForConstantValue:[NSNumber numberWithBool:YES]]
modifier:NSDirectPredicateModifier type:NSEqualToPredicateOperatorType
options:0];

NSArray *newArray = [myArrayOfObjects
filteredArrayUsingPredicate:searchPredicate];


--
Jim
http://nukethemfromorbit.com
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >filteredArrayUsingPredicate and points (From: Alexey Baev <email@hidden>)

  • Prev by Date: [ANN] ObjectiveFlickr 2.0, a Flickr API framework for Mac and iPhone
  • Next by Date: Master-detail interface, multiple selection, and mixed state checkbox state transitions
  • Previous by thread: Re: filteredArrayUsingPredicate and points
  • Next by thread: RE:filteredArrayUsingPredicate and points
  • Index(es):
    • Date
    • Thread