• 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: Use of blocks
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Use of blocks


  • Subject: Re: Use of blocks
  • From: Preston Sumner <email@hidden>
  • Date: Mon, 12 Jul 2010 02:32:50 -0600

You can use key-path collection operators, assuming someTrips is a property of self:

    NSNumber *minPrice = [self valueForKeyPath:@"email@hiddenFinalPrice"];
    NSNumber *maxPrice = [self valueForKeyPath:@"email@hiddenFinalPrice"];

By the way, minValue and maxValue in your example need to be declared with __block or they'll be captured as read-only constants.

Preston Sumner

On Jul 12, 2010, at 1:34 AM, Giannandrea Castaldi wrote:

> Hi,
> I want to use blocks to extract the min price and the max price from
> an NSArray of trips. I've found the following way:
>
>    float minValue = 0.0;
>    [someTrips enumerateObjectsUsingBlock:^(id obj, NSUInteger idx,
> BOOL *stop) {
>      if (minValue == 0.0 || [[obj adultFinalPrice] floatValue] < minValue) {
>        minValue = [obj floatValue];
>      }
>    }];
>    float maxValue = 0.0;
>    [someTrips enumerateObjectsUsingBlock:^(id obj, NSUInteger idx,
> BOOL *stop) {
>      if (maxValue == 0.0 || [[obj adultFinalPrice] floatValue] > maxValue) {
>        maxValue = [obj floatValue];
>      }
>    }];
>
> I've just begun to use blocks and I wonder if there is a more brief
> way, may be without the need to define the float variables before the
> iterations.
>
> I've also looked for a solution with NSExpression and NSPredicate with
> the function min/max but if I've correctly understood the use of
> NSExpression and NSPredicate with such functions is only for CoreData.
> Thanks.
>
> Jean
> --
> “We are what we repeatedly do. Excellence then, is not an act, but a habit.”
> Aristotle
> _______________________________________________
>
> 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

_______________________________________________

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: 
 >Use of blocks (From: Giannandrea Castaldi <email@hidden>)

  • Prev by Date: Re: Use of blocks
  • Next by Date: Re: Use of blocks
  • Previous by thread: Re: Use of blocks
  • Next by thread: Re: Use of blocks
  • Index(es):
    • Date
    • Thread