0

I have an array with double values that can range from 0.0 to 100.0. I would like to alert the user if any of the values are below 10.0. I did some searching and the closest thing I could find was:

- (BOOL)containsObject:(id)anObject

Is there any way I could use this method to see if the values are below 10? I tried the following line of code but received two errors.

if ([myArray containsObject:[NSNumber numberWithDouble:(<10)])
{
    // Do something
}

I would appreciate the assistance. It seems like a pretty basic task.

4 Answers4

5

If it is not an array with lots of data, I don't see the reason why wouldn't you do it like this:

   for (NSNumber *number in myArray) {
        if ([number floatValue] < 10.0) {
            // alert user 
        }
    }
JPetric
  • 3,838
  • 28
  • 26
  • 2
    Even for a large (unsorted) array this is the most effective solution. – Martin R Aug 15 '13 at 13:19
  • @MartinR even compared to using an NSPredicate? – sbarow Aug 15 '13 at 13:25
  • @sbarow: With NSPredicate you allocate and fill a new array, so I *assume* that a simple enumeration is faster. – Martin R Aug 15 '13 at 13:31
  • The speed difference will be negligible unless you're dealing with an insanely large array. NSPredicate probably does something similar to what the answer does, or it may sort it then go through it afterwards (unless there's some magic going on in the background), either way unless you're dealing with a large amount of data the user won't know the difference. The answer above is a simple and easy to understand solution that can be applied to other programming languages, NSPredicate can't (although many languages have their own version, but it requires you to look it up) – Matt S. Aug 15 '13 at 13:37
1

Use NSPredicate for more faster

NSPredicate *predicatePeople =  [NSPredicate predicateWithFormat:@"(startValue => %f) BETWEEN (endValue <= %f))",startNSNumber,endNSNumber];

NSArray *filteredArray = [myArray filteredArrayUsingPredicate:predicatePeople];
if(filteredArray.count > 0)
{
  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Information" message:@"MyArray Contain between 1 to 10 value" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
     [alert show];
}
iPatel
  • 46,010
  • 16
  • 115
  • 137
  • Why do you think that NSPredicate is faster? I would assume that *creating a new array* is slower than a simple enumeration. - Btw. the usage of BETWEEN in your predicate is wrong. – Martin R Aug 15 '13 at 13:28
0

Also, the usage of containsObject: is for "equals" purpose, like [myArray containsObject:[NSNumber numberWithDouble:2.0]], not conditional like you did. So, the best is what JPetric mentioned.

for (NSNumber *number in myArray) {
        if ([number floatValue] < 10.0) {
            // alert user 
        }
    }
Davi Stuart
  • 269
  • 3
  • 16
0

I would use blocks, they are cooler and it will help to familiarize yourself with them:

[myArray enumerateObjectsWithBlock^(NSNumber *number, NSUInteger indx, BOOL *stop) {
    if ([number floatValue] < 10.0) {
        // alert user

        // kill the loop to prevent unnecessarily going through all the elements
        stop = YES;
    }
}];

If you do want to user fast enumeration include a break:

for (NSNumber *number in myArray) {
     if ([number floatValue] < 10.0) 
           // alert user 
           break; // kills the loop
     }
 }

If you only care if one item is under 10 these approaches work well because you do not need to go through every element every time. Also, if this is the case, predicates are not efficient because they will filter the whole array when in reality you only care about one item (the first item under 10).

Firo
  • 15,448
  • 3
  • 54
  • 74
  • Block enumeration *is* cool, but can be *slower* than fast enumeration, compare http://blog.bignerdranch.com/2337-incremental-arrayification/ or http://stackoverflow.com/a/15931719/1187415. – Martin R Aug 15 '13 at 13:42
  • Yeah I guess, seems a bit controversial. I just remember in Apple's WWDC 2010 videos they said it was more efficient and quicker. I guess this is primarily true when the blocks are not simplistic, and when multi-core options are taken, thanks for the links! (I updated my answer) – Firo Aug 15 '13 at 13:52