EXC_BAD_ACCESS description method


#1

Hi,

I wrote down the Possession class like this:

//
//  Possession.m
//  RandomPossession
//
//  Created by Matteo Lavoro on 30/11/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "Possession.h"

@implementation Possession



//getters
-(NSString*) possessionName{
    return possessionName;
}
-(NSString*) serialNumber{
    return serialNumber;
}
-(int) valueInDollars{
    return valueInDollars;
}
-(NSDate*) dateCreated{
    return dateCreated;
}

//setters
-(void) setPossessionName :(NSString*) newName{
    possessionName = newName;
}
-(void) setSerialNumber :(NSString*) newSerial{
    serialNumber = newSerial;
}
-(void) setValueInDollars :(int) newValue{
    valueInDollars = newValue;
}

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

@end

but when I execute

        Possession* p = [[Possession alloc] init];
        [p setPossessionName:@"RedSofa"];
        [p setSerialNumber:@"01239"];
        [p setValueInDollars:100];
        
//        NSLog(@"%@ %@ %d %@", [p possessionName], [p serialNumber], [p valueInDollars], [p dateCreated]);
        
        NSLog(@"%@", p);

I receive an EXC_BAD_ACCESS and the debugger stops into the description method of Possession.

I can’t find the error… can someone helps me?

Thanks!


#2

Where you have --> initWithFormat:@"%@ (%@) worth: @d on %@" <-- , think it should be %d for the integer valueInDollars.


#3

:blush: this is very noob :smiley:

Thank you!