-2

Hi there I have some code at the moment that gives me the error ("0") is not equal to ("50") - condition not applied correctly Basically I am currently using a traditional for loop within a BOOL which goes through the list of items and checks whether or not the condition can be applied, if it can be applied to an item then the BOOL will return YES. I cannot see where I am currently going wrong and need guidance. My code is shown below:

-(BOOL)conditionCanBeApplied:(NSArray *)items{
       bool itemConditionCanBeApplied = NO;
       for (int i = 0; i < items.count; i++) 
       {
            id myItem = [[items valueForKeyPath:@"itemId"]objectAtIndex: i];
             if ([self.applicableItems containsObject:myItem]) {
                    itemConditionCanBeApplied = YES;
              }
       }   
            return itemConditionCanBeApplied;   
}
Hima
  • 1,249
  • 1
  • 14
  • 18
Jim
  • 1
  • 2
  • Did the copy/paste go bad, because you're trying to do `valueForKeyPath:` on an `NSNumber`. – Jon Shier Dec 04 '14 at 20:03
  • There are many things wrong with the code inside the loop. `number2` is set to `nil` so `number1` will always be `nil`. You never use any values from the `items` array. – rmaddy Dec 04 '14 at 20:04
  • 1
    Nor is it very clear what this method is trying to do, or what "condition can be applied" means in this context. – Jon Shier Dec 04 '14 at 20:05
  • I am trying to go through the items within the 'items' array and check whether the condition e.g. discount can be applied to the item. How would I go about at making this work. I've edited the code to the above. – Jim Dec 04 '14 at 20:15
  • Why is `myItem` of type `id`? What kind of objects are really in the array? Why do you assign `myItem` to one of the array elements and then another value on the next line? – rmaddy Dec 04 '14 at 20:26
  • What is `myItem = [items valueForKeyPath:@"itemId"];` supposed to be doing? – Phillip Mills Dec 04 '14 at 20:28
  • its because I am not sure myself. The items are tools stored within a plist that have three main properties, 'name', 'itemid' and 'price'. I am trying to decipher from using the 'itemid' which are numbers 0-10; which items can have discount applied. e.g. only item 4 and 5 can have discount. – Jim Dec 04 '14 at 20:36
  • Your prose doesn't make any sense, but based on your code, you're trying to do this: [How do I search an NSArray of objects for an object whose NSString properties match?](http://stackoverflow.com/q/25018159) – jscs Dec 04 '14 at 22:03

1 Answers1

0

First, don't mix BOOL and bool, they might be very similar but they aren't the same data type. Second, always use fast enumeration if you have a choice. I am assuming in the code below that your items collection is something like an NSArray. Also, there is no reason to test with an if statement just to set a BOOL since the test is a boolean statement. (I am doing it in my example to allow for the break) Lastly, short-circuiting your logic with a break keeps the processor from doing unnecessary work once you have at least one match.

Do something like this:

- (BOOL)conditionTest 
{
    BOOL itemConditionCanBeApplied = NO;
    for (id item in items) {
        if ([self.applicableItems containsObject:item]) {
            itemConditionCanBeApplied = YES;
            break;
        }
    }

    return itemConditionCanBeApplied;
}
SefTarbell
  • 342
  • 2
  • 12
  • Thanks for answer that it useful knowledge the only thing is that say I have items @4 and @5 that the condition is applied to. How would I go about accessing the 'itemId to check each items 'itemId' and if the item has itemId 4 or 5 the discount will be applied? the 'itemId' is an integer property defined in another class within my project? – Jim Dec 04 '14 at 21:30