Predicate example


#1

My NSArray *toBeReclaimed shows nothing in the console.

Using breakpoints, I found that when I create toBeReclaimed, it has no values. How can that be? All of my values are greater than 70.

[code]#import <Foundation/Foundation.h>
#import “BNREmployee.h”
#import “BNRAsset.h”

int main(int argc, const char * argv[])
{

@autoreleasepool {
    
    // Create an array of BNREMployees objects
    NSMutableArray *employees = [[NSMutableArray alloc]init];
    
    NSMutableDictionary *executives = [[NSMutableDictionary alloc]init];
    
    for (int i = 0; i < 10; i++) {
        //create an instance of BNREmployee
        BNREmployee *mikey = [[BNREmployee alloc]init];
        
        //give the instance vars interesting values
        mikey.weightInKilos = 90 + i;
        mikey.heightInMeters = 1.8 - i/10.0;
        mikey.employeeID = i;
        
        //put the employee into the array
        [employees addObject:mikey];
        
        //is this the first employee?
        if (i ==0) {
            [executives setObject:mikey forKey:@"CEO"];
        }
        
        //is this a second employee?
        if (i == 1) {
            [executives setObject:mikey forKey:@"CTO"];
        }
    }
    
    NSMutableArray *allAssets = [[NSMutableArray alloc]init];
    //create 10 assets
    
    for (int i = 0; i < 10; i++) {
        //create an asset
        BNRAsset *asset = [[BNRAsset alloc]init];
        
        //give it an interesting label
        NSString *currentLabel = [NSString stringWithFormat:@"Laptop %d", i];
        asset.label = currentLabel;
        asset.resaleValue = 350 +  i * 17;
        
        //get a random number between 0 and 9
        NSUInteger randomIndex = random() % [employees count];
        
        //Find that employee
        BNREmployee *randomEmployee = [employees objectAtIndex:randomIndex];
        
        [randomEmployee addAsset:asset];
        
        [allAssets addObject:asset];
        
    }
    
    NSSortDescriptor *voa = [NSSortDescriptor sortDescriptorWithKey:@"valueOfAssets" ascending:YES];
    
    NSSortDescriptor *eid = [NSSortDescriptor sortDescriptorWithKey:@"employeeID" ascending:YES];
    
    [employees sortUsingDescriptors:@[voa, eid]];
    
    NSLog(@"Employee: %@", employees);
    
    NSLog(@"Giving up ownership of one employee");
    
    [employees removeObjectAtIndex:5];
    
    NSLog(@"allAssets: %@", allAssets);
    
    //print out the entire dictionary
    
    NSLog(@"executives: %@", executives);
    
    
    
    
    //Print out the CEO info
    
    NSLog(@"CEO: %@", executives[@"CEO"]);
    executives = nil;
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"holder.valueOfAssets > 70"];
    
    NSArray *toBeReclaimed = [allAssets filteredArrayUsingPredicate:predicate];
    
    NSLog(@"toBeReclaimed: %@", toBeReclaimed);
    
    toBeReclaimed = nil;
    
    NSLog(@"giving up ownership of arrays");
    
    employees = nil;
    
    allAssets = nil;
    
    
}
return 0;

}
[/code]