2010-08-31 44 views
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时,稍后会出现此问题。它突然把我wineNameUIImageView而非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; 
} 

有什么想法吗?

+2

是w.wineName是一个retain'ed属性?这听起来有点像字符串被自动释放。你可以发布Wine对象的定义吗? – RichB 2010-08-31 13:35:54

+0

您对'wineName'属性的setter或property声明是什么样的? – Sven 2010-08-31 13:39:33

+0

非常感谢您的指点。我正在与另一个开发人员一起工作,它是'@property(nonatomic,assign)NSString * wineName;'但是,将它改为'@property(nonatomic,retain)NSString * wineName;'完美地工作。 – rson 2010-08-31 13:41:47

回答

0

通过RichB解决问题的意见:

w.wineName一个retain'ed财产?这听起来有点像字符串被自动释放。你可以发布Wine对象的定义吗?