我剛剛花了最後3個小時試圖找出這個錯誤。我希望有人向我解釋,所以我不再做。iphone分配實例變量
我分配了一個NSString實例變量而不使用「self」。當類(「自我」)發佈時,我收到了「訪問不良」錯誤。我已經在另一個具有相同變量聲明的類中做了完全相同的事情,並且沒有這個錯誤。以下是我的代碼。我評論了這條線斷了,下面的線修復了它。但我不明白爲什麼......請注意,還有其他實例變量不會導致此問題。我應該在分配實例變量時總是使用「自我」保留字嗎?請告訴我。
聲明
@property (nonatomic, readonly, assign) int IID;
@property (nonatomic, assign) int ProfileIID;
@property (nonatomic, retain) NSDate *NoteDate;
@property (nonatomic, copy) NSString *NoteText;
代碼段
// the default date format is Year-Month-Day
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:kDateFormat];
IID = sqlite3_column_int(selectstmt, 0);
ProfileIID = sqlite3_column_int(selectstmt, 1);
// notice this does not cause a memory error
NoteDate = [[df dateFromString: [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)]] retain];
// the following may be NULL. Checking using the sqlite3_column_text method
const char *columnText = (const char *)sqlite3_column_text(selectstmt, 3);
if(columnText != NULL)
{
// this causes a memory error
//NoteText = [NSString stringWithUTF8String: columnText ];
// this does not cause memory error
self.NoteText = [NSString stringWithUTF8String: columnText ];
}
該死的,我只是輸入了這個xD Nice的迴應。 – 2009-06-14 19:53:58