Use of undeclared identifier 'date created'


#1

In 3rd chapter you advise to delete explicit declaration of instance variables(instead we added properties), but after this built was failed as compiler couldn’t find declaration of dateCreated variable in this part of code:

[code]- (id)initWithItemName:(NSString *)name
valueInDollars:(int)value
serialNumber:(NSString *)sNumber
{
self = [super init];

if (self) {
[self setItemName:name];
[self setSerialNumber:sNumber];
[self setValueInDollars:value];

dateCreated = [[NSDate alloc] init]; // use of undeclared identifier ‘date created’ (!)
}

return self;

}
[/code]
In spite of I added property for dateCreated and synthesized it

So I had to declare it explicity in BNRItem.h as it was before and then built was succeeded. Can you tell me what I did wrong? Thank you in advance!


#2

Hm, can you post the entire .h and .m files exactly from your project?


#3

of course!

[code]//

#import <Foundation/Foundation.h>

@interface BNRItem : NSObject
{
NSDate *dateCreated;
}

// class method

  • (id)randomItem;

// designated initializer

  • (id)initWithItemName:(NSString *)name
    valueInDollars:(int)value
    serialNumber:(NSString *)sNumber;

// accessors
@property (nonatomic, strong) BNRItem *containedItem;
@property (nonatomic, weak) BNRItem *container;

@property (nonatomic, copy) NSString *itemName;
@property (nonatomic, copy) NSString *serialNumber;
@property (nonatomic) int valueInDollars;
@property (nonatomic, readonly, strong) NSDate *dateCreated;

@end
[/code]

[code]//

#import “BNRItem.h”

@implementation BNRItem

+(id)randomItem
{
// create an array with three adjectives
NSArray *randomAdjectiveList = [NSArray arrayWithObjects:@“Fluffy”,
@“Rusty”,
@“Shiny”, nil];
// create an array with three nouns
NSArray *randomNounList = [NSArray arrayWithObjects:@“Bear”,
@“Spork”,
@“Mac”, nil];

NSInteger adjectiveIndex = rand() % [randomAdjectiveList count];
NSInteger 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];
BNRItem *newItem = [[self alloc] initWithItemName:randomName
                                   valueInDollars:randomValue 
                                     serialNumber:randomSerialNumber];

return newItem;

}

// designated initializer

  • (id)initWithItemName:(NSString *)name
    valueInDollars:(int)value
    serialNumber:(NSString *)sNumber
    {
    self = [super init];

    if (self) {
    [self setItemName:name];
    [self setSerialNumber:sNumber];
    [self setValueInDollars:value];
    dateCreated = [[NSDate alloc] init];
    }

    return self;

}

// overriding superclass init method

  • (id)init
    {
    return [self initWithItemName:@“Item"
    valueInDollars:0
    serialNumber:@”"];
    }

// accessors
@synthesize itemName;
@synthesize serialNumber, valueInDollars, dateCreated, containedItem, container;

  • (void)setContainedItem:(BNRItem *)i
    {
    containedItem = i;
    [i setContainer:self];
    }

// overriding superclass methods descriprion and dealloc

  • (NSString *)description
    {
    NSString *stringDescription = [[NSString alloc] initWithFormat:@"%@(%@): Worth $%d recorded on %@",
    itemName, serialNumber,
    valueInDollars, dateCreated];

    return stringDescription;
    }

-(void)dealloc
{
NSLog(@“Destroyed: %@”, self);
}

@end
[/code]


#4

That’s weird. Sometimes, Xcode decides not to hide errors after you’ve fixed them, especially if you have the “compile as you type” feature on.

Try this - remove the instance variable, move the synthesize statements to the top of the implementation block, clean the project, and build again?


#5

Thank you! After moving synthesize statements to the top(so now it is before the line where ‘dateCreated’ is being used ) it works properly without explicit declaration!