[英]Chat App, allocation issue. Error on custom class initialization in Xcode (EXC_BAD_ACCESS code=2)
[英]EXC_BAD_ACCESS code=2 error on custom class initialization in Xcode
我正在嘗試在我的視圖控制器中初始化自定義類以管理數據。 不幸的是,該應用在加載過程中崩潰了。 我正在使用lldb調試器運行Xcode 5.02。 我得到的錯誤是
Thread 1: EXC_BAD_ACCESS (code=2, address=0xbf7ffffc)
錯誤顯示在函數的第一行(-(void)...)
-(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]];
}
}
當對象初始化時調用該方法
-(id)initWithDate:(NSDate *)date
{
self = [super init];
if(self) {
[self setDateOfErgPiece:date];
}
return self;
}
從此方法接收上述init方法中的(NSDate *)date值
-(ErgNewDataEntryLogic *)ergPieceData {
if(!_ergPieceData) _ergPieceData = [[ErgNewDataEntryLogic alloc] initWithDate:[NSDate date]];
return _ergPieceData;
}
是什么導致此錯誤? 如果您需要更多信息,我們很樂意提供。 非常感謝!
我不知道這到底是不是問題,但這當然是一個主要問題:
-(void)setDateOfErgPiece:(NSDate *)date {
self.dateOfErgPiece = date;
// ...
self.dateOfErgPiece = date;
完全等效於[self setDateOfErgPiece:date];
。
因此,作為方法的第一行,該方法正在調用自身。 無限遞歸。
這應該更改為以下內容...
-(void)setDateOfErgPiece:(NSDate *)date {
_dateOfErgPiece = date;
// ...
方法中對self.dateOfErgPiece
的其他引用似乎還可以,因為它們看起來像是在調用getter:
[self dateOfErgPiece];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.