我刚刚花了最后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