Challenge solution


#1

Here’s my go at the challenge solution.

I’m only showing main.m and my console output.

main.m

//
//  main.m
//  randomPossessions
//
//  Created by Kurt Farao on 2012/02/27.
//  Copyright (c) 2012 kurt_farao@yahoo.com. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Possession.h"

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

    @autoreleasepool {
        // create a mutable array, store it's address in the items array
        NSMutableArray *items = [[NSMutableArray alloc] init];
        
        for (int i = 0; i < 10; i++){
            Possession *p = [Possession randomPossession];
            [items addObject:p];
        }
              
        NSString *eleventhItem = [items objectAtIndex:11];
        
        NSLog(@"Eleventh: %@", eleventhItem);
        
        
        //for (NSString *k in items){
          //  NSLog(@"%@", k);
        //}
        
        // release the array
        [items release];
        
        // dont leave items pointing at freed memory
        items = nil;     
        
    }
    return 0;
}

Console output

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 11 beyond bounds [0 .. 9]'

I guess this is correct. :slight_smile: