سؤال

I'm attempting to initialize a custom class in my view controller to manage the data. Unfortunately the app crashes during the loading. I'm running Xcode 5.02 with the lldb debugger. The error I get is

Thread 1: EXC_BAD_ACCESS (code=2, address=0xbf7ffffc)

The error shows up on the first line (-(void)...) of the function

    -(void)setDateOfErgPiece:(NSDate *)date
{
    self.dateOfErgPiece = date;

    if(self.dateOfErgPiece) {
        // Date Formatter. So Date is displayed correctly
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateStyle:NSDateFormatterShortStyle];
        [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
        // Set value
        self.dateOfErgPieceString = [NSString stringWithFormat:@"%@", [dateFormatter stringFromDate:self.dateOfErgPiece]];
    }

}

That method is called when the object is initialized by

-(id)initWithDate:(NSDate *)date
{
    self = [super init];

    if(self) {
        [self setDateOfErgPiece:date];
    }

    return self;
}

The (NSDate *)date value in the above init method is received from this method

-(ErgNewDataEntryLogic *)ergPieceData {
    if(!_ergPieceData) _ergPieceData = [[ErgNewDataEntryLogic alloc] initWithDate:[NSDate date]];
    return _ergPieceData;
}

What is causing this error? If you need more information I'd be happy to provide it. Thank you so much!

هل كانت مفيدة؟

المحلول

I don't know if this is exactly the problem or not, but this is certainly a major problem:

-(void)setDateOfErgPiece:(NSDate *)date {
    self.dateOfErgPiece = date;
    // ...

self.dateOfErgPiece = date; is exactly equivalent to [self setDateOfErgPiece:date];.

So, as the first line of the method, the method is calling itself. Infinite recursion.

This should be changed to the following...

-(void)setDateOfErgPiece:(NSDate *)date {
    _dateOfErgPiece = date;
    // ...

The other references to self.dateOfErgPiece within the method seem to be okay, because they look to be calling the getter:

[self dateOfErgPiece];
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top