Can't find whats wrong with my code p.121


#1

Hi guys,

I’m having trouble finding the problem in my code. When I build the program it runs forever and never finishes building. I’ve copied and pasted others code and runs fine. I can seem to find how mine is different from the others. If someone can spot it, I would appreciate it.

Thanks,

#import <Foundation/Foundation.h>
#import "Person.h"
#import "Employee.h"
#import "Asset.h"

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

    @autoreleasepool {
        
        //Create an array of Employee objects
        NSMutableArray *employees = [[NSMutableArray alloc] init];
        
        for (int i = 0; 1<10; i++) {
            //create an instarnce of Employee class
            Employee *person = [[Employee alloc] init];
            
            //Give the instance variables interesting values
            [person setWeightInKilos:90 + i];
            [person setHeightInMeters:1.8 - i/10];
            [person setEmployeeID:i];
            
            //add employee in the employees array
            [employees addObject:person];
            
      }
        
        
        //Create 10 assets
        for (int i = 0; i < 10; i++) {
            
            //create an asset
            Asset *asset = [[Asset alloc] init];
            
            //give it an interesting label
            NSString *currentLabel = [NSString stringWithFormat:@"Laptop %d",i];
            [asset setLabel:currentLabel];
            [asset setResaleValue:i * 17];
            
            //get a random number between 0 and 9 inclusive
            NSUInteger randomIndex = random() % [employees count];
            
            //Find that employee
            Employee *randomEmployee = [employees objectAtIndex: randomIndex];
            
            //Asign the asset to the employee
            [randomEmployee addAssetObject:asset];
            
        }
        
        NSLog(@"Employees %@", employees);
        
        NSLog(@"Giving up ownership of one employee");
        
        [employees removeObjectAtIndex:5];
        
        NSLog(@"Giving up ownership of array");
        
        employees = nil;
    
    }
    return 0;
}


#2

Looks like the condition in your first for loop says 1<10, which is always true, rather than i <10. So you never complete the first loop.


#3

Thanks alearner! I just couldn’t figure out what I was overlooking.