Random Possessions not working


#1

I have typed in the randomPossession class method but I can’t get it to work. When I view the debug console I only get the following:

GNU gdb 6.3.50-20050815 (Apple version gdb-1708) (Thu Nov 3 21:59:02 UTC 2011)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type “show copying” to see the conditions.
There is absolutely no warranty for GDB. Type “show warranty” for details.
This GDB was configured as “x86_64-apple-darwin”.tty /dev/ttys000
sharedlibrary apply-load-rules all
[Switching to process 1668 thread 0x0]

I’ve checked and rechecked but I can’t seem to spot the error. Can someone help? I’ve added main, possessions.h, and possessions.m below.

Thanks in advance

//
//  main.m
//  Random Posessions
//
//  Created by Me on 1/11/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

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

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

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    // Create a mutable array, store its address in items variable
    NSMutableArray *items = [[NSMutableArray alloc]init];
    
    for (int i = 0; i < [items count]; i++)
    {
        Possession *p = [Possession randomPossession];
        [items addObject:p];
        //NSLog(@"%@", [items objectAtIndex:i]);
    }
    
    for(int i = 0; i < [items count]; i++)
    {
        NSLog(@"%@", [items objectAtIndex:i]);
    }
                
    //Release the array
    [items release];
        
    //Don't leave items pointing at freed memory:
    items = nil;
        
    [pool drain];
    
    return 0;
}
//
//  Posession.h
//  Random Posessions
//
//  Created by Me on 1/11/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Possession : NSObject
{
    NSString *possessionName;
    NSString *serialNumber;
    int valueInDollars;
    NSDate *dateCreated;
}

+ (id)randomPossession;

- (id)initWithPossessionName:(NSString *)name
              valueInDollars:(int)value
                serialNumber:(NSString *)sNumber;

- (void)setPossessionName:(NSString *)str;
- (NSString *)possessionName;

- (void)setSerialNumber:(NSString *)str;
- (NSString *)serialNumber;

- (void)setValueInDollars:(int)i;
- (int)valueInDollars;

- (NSDate *)dateCreated;

@end
//
//  Posession.m
//  Random Posessions
//
//  Created by me on 1/11/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "Possession.h"

@implementation Possession

+(id)randomPossession
{
    // Create an array of three adjectives
    NSArray *randomAdjectiveList = [NSArray arrayWithObjects:@"Fluffy", @"Rusty", @"Shiny", nil];                                       
    // Create an array of three nouns
    NSArray *randomNounList = [NSArray arrayWithObjects:@"Bear", @"Spork", @"Mac", nil];
    
    // Get the index of a random adjective/noun from the lists
    // Note:  The % operator, called the modulao operator, gives
    // you the remainder.  So adjectiveINdex is a random number
    // from 0 to 2 inclusive.
    int adjectiveIndex =  rand() % [randomAdjectiveList count];
    int nounIndex = rand() % [randomNounList count];
    
    NSString *randomName = [NSString stringWithFormat:@"%@ %@",
                            [randomAdjectiveList objectAtIndex:adjectiveIndex],
                            [randomNounList objectAtIndex:nounIndex]];
    
    int randomValue = rand() % 100;
    
    NSString *randomSerialNumber = [NSString stringWithFormat:@"%c%c%c%c%c",
                                    '0' + rand() % 10,
                                    'A' + rand() % 26,
                                    '0' + rand() % 10,
                                    'A' + rand() % 26,
                                    '0' + rand() % 10];
    
    Possession *newPossession = [[self alloc] initWithPossessionName:randomName
                                                      valueInDollars:randomValue
                                                        serialNumber:randomSerialNumber];
    return newPossession;
}

- (id)init
{
    return [self initWithPossessionName:@"Possession"
                         valueInDollars:0
                           serialNumber:@""];
}
        
- (id)initWithPossessionName:(NSString *)name
              valueInDollars:(int)value
                serialNumber:(NSString *)sNumber
{
    // Call the superclass' designated initializer
    self = [super init];
    
    // Did the superclass's designated initializer succeed?
    if (self) {
    
        // Give the instance variables initial vlaues
        [self setPossessionName:name];
        [self setSerialNumber:sNumber];
        [self setValueInDollars:value];
        dateCreated = [[NSDate alloc] init];
        
    }
    
    // Return the address of the newly initialized object
    return self;
}

- (void)setPossessionName:(NSString *)str 
{
    possessionName = str; 
}
- (NSString *)possessionName
{
    return possessionName;
}

- (void)setSerialNumber:(NSString *) str
{
    serialNumber = str;
}
- (NSString *)serialNumber
{
    return serialNumber;
}

- (void)setValueInDollars:(int)i
{
    valueInDollars = i;
}
- (int)valueInDollars
{
    return valueInDollars;
}

- (NSDate *)dateCreated
{
    return dateCreated;
}

- (NSString *)description
{
    NSString *descriptionString = [[NSString alloc] initWithFormat:@"%@ (%@): Worth $%d, recorded on %@",
                                   possessionName,
                                   serialNumber,
                                   valueInDollars,
                                  dateCreated];
    return descriptionString;
}
@end

#2

Hello madsenb,

take a look at the first code-snippet (main.m). You can find the mistake in your first for-loop.
You wrote this:

simply try this:

this should work. Here is why:
The code inside your first for-loop doesn’t get executed, because [items count] is zero.
Your array is empty at this point. This isn’t really a logical error, just a typing-mistake.

I hope I could help. :slight_smile:

You can find the correct code on page 41 in the book.