NSObject 属性的奇怪行为

发布于 2024-09-16 20:40:08 字数 2392 浏览 8 评论 0原文

我有一个 NSObject,用于存储/保存对象的属性,其中之一是转换为 NSString 的“Name”属性。我还使用以下命令从 SQLite DB 中提取该对象的数据:

- (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.
    }

Wine 是我的对象。如果我此时记录 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;
}

有什么想法吗?

I have an NSObject that I am using to store/hold properties of an object, one of them being a "Name" property cast as an NSString. I am also pulling data from a SQLite DB for said object using the following:

- (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.
    }

Wine being my object. If I were to log w.wineName at this point, there is no problem. The problem occurs later when I try to access the properties of the object from within the array, dataManager.wines, in a custom tableView. It suddenly treats my wineName as a UIImageView rather than an NSString...

I for the life of me cannot trace anything back to ever being cast as a UIImageView and have no idea why it would be setting just that property as such. Here is the code for my custom 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;
}

any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冬天旳寂寞 2024-09-23 20:40:08

在 RichB 的问题评论中解决:

w.wineName 是一个保留的属性吗?这听起来有点像弦被自动释放。您能发布 Wine 对象的定义吗?

Resolved in comments of question by RichB:

is w.wineName a retain'ed property ? This sounds a bit like the string is being auto released. Could you post the definition of the Wine object?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文