0
我有一個NSObject
,我用它來存儲/保存一個對象的屬性,其中之一是作爲NSString
鑄造的「名稱」屬性。我也使用以下方法從SQLite數據庫中提取數據:NSObject屬性的奇怪行爲
- (void) getDataToDisplay:(NSString *)dbPath {
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
NSString *queryOnStyle = [NSString stringWithFormat:
@"SELECT WineId, Name FROM wine WHERE StyleId = %d", dataManager.styleId];
const char *sql = [queryOnStyle UTF8String];
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
Wine *w = [[Wine alloc] init];
w.wineId = sqlite3_column_int(selectstmt, 0);
w.wineName = [NSString stringWithUTF8String:
(char *)sqlite3_column_text(selectstmt, 1)];
[dataManager.wines addObject:w];
[w release];
}
}
}
else
sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
}
酒是我的對象。如果我此時登錄w.wineName
,則沒有問題。當我嘗試在自定義tableView中訪問數組中的對象的屬性dataManager.wines
時,稍後會出現此問題。它突然把我wineName
爲UIImageView
而非NSString
...
我爲我的生活不能跟蹤任何東西永遠被鑄造爲UIImageView
,而且不知道爲什麼它會被設置只是財產等。這裏是我自定義tableView的代碼:
#pragma mark -
#pragma mark HorizontalTableViewDelegate methods
- (NSInteger)numberOfColumnsForTableView:(HorizontalTableView *)tableView {
return [dataManager.wines count];
}
- (UIView *)tableView:(HorizontalTableView *)aTableView viewForIndex:(NSInteger)index {
UIView *vw = [aTableView dequeueColumnView];
if (!vw) {
[[NSBundle mainBundle] loadNibNamed:@"ColumnView" owner:self options:nil];
vw = self.columnView;
self.columnView = nil;
}
// Get the wineId from the array of wineId integers
Wine *w = [dataManager.wines objectAtIndex:index];
int tempWineId = w.wineId;
NSString *tempWineName = [NSString stringWithFormat:@"%@", w.wineName];
NSLog(@"%@", tempWineName); \\RETURNS TEMPWINENAME AS A UIIMAGEVIEW
[w release];
return vw;
}
- (CGFloat)columnWidthForTableView:(HorizontalTableView *)tableView {
//TODO: This value needs to change if changed in IB
return 209.0f;
}
有什麼想法嗎?
是w.wineName是一個retain'ed屬性?這聽起來有點像字符串被自動釋放。你可以發佈Wine對象的定義嗎? – RichB 2010-08-31 13:35:54
您對'wineName'屬性的setter或property聲明是什麼樣的? – Sven 2010-08-31 13:39:33
非常感謝您的指點。我正在與另一個開發人員一起工作,它是'@property(nonatomic,assign)NSString * wineName;'但是,將它改爲'@property(nonatomic,retain)NSString * wineName;'完美地工作。 – rson 2010-08-31 13:41:47