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 Answers 4

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 
        }
    }
Sign up to request clarification or add additional context in comments.

4 Comments

Even for a large (unsorted) array this is the most effective solution.
@MartinR even compared to using an NSPredicate?
@sbarow: With NSPredicate you allocate and fill a new array, so I assume that a simple enumeration is faster.
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)
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];
}

1 Comment

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.
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 
        }
    }

Comments

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).

2 Comments

Block enumeration is cool, but can be slower than fast enumeration, compare blog.bignerdranch.com/2337-incremental-arrayification or stackoverflow.com/a/15931719/1187415.
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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.