0

I have a database manager which successfully loads a string from my sqlite database, and then uses it to create an object. I add multiple of these objects to an array and return it to which ever class I'm calling it from.

I have no problem using the returned array in the method I call it from, but when other methods in the class try to access it, the string element is out of scope.

Here is my relavent code.

In DBManager.m:

-(NSArray *)loadObjectData {
    NSMutableArray *myArray = [[NSMutableArray alloc] init];

    //prepare query and statement
    NSString *query = [NSString stringWithFormat:@"SELECT * FROM object;"];
    sqlite3_stmt *statement;

    //perform query
    if (sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil) != SQLITE_OK)
        NSLog(@"Prepare Error. '%s'", sqlite3_errmsg(database));

    //get data
    while (sqlite3_step(statement) == SQLITE_ROW) {
        //read values
        int anInt = sqlite3_column_int(statement, 0);
        NSString *aString = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
        //add new object to list
        [myArray addObject:[[Object alloc] initWithID:anInt Name:aString]];
    }
    if(sqlite3_finalize(statement) != SQLITE_OK)
        NSLog(@"Finalize statement error.");

    return [myArray autorelease];
}

In XViewController.h:

NSArray *listData;
SessionData *sessionData;

@property (nonatomic, retain) NSArray *listData;
@property (nonatomic, retain) SessionData *sessionData;

In XViewController.m:

- (void)viewDidLoad
{
    dbMan = [DBManager sharedDBMananager]; //its a singleton

    //Load objects to display in table
    self.listData = [dbMan loadObjectData];

    //if i try to use listData here, it works fine.
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //normal stuff cut out

    // Configure the cell...
    //the following line fails because the string in the object out of scope
    cell.textLabel.text = [[listData objectAtIndex:[indexPath row]] aString];
    //trying to access [[listData objectAtIndex:[indexPath row]] anInt] works fine though.

    return cell;
}

If I replace the line (in my loadObjectData method):

NSString *aString = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)];

with this:

NSString *aString = @"String";

everything works fine.

2 Answers2

0

In Object class have you retained this string if not then i think you should and release this string in dealloc

Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
0

Take a look at this question's accepted answer - What does assigning a literal string to an NSString with "=" actually do?

The string literal ( @"String" ) works because it is statically allocated and does not need to be retained/released. [NSString stringWithUTF8String:] creates an autoreleased string. If you are not retaining it in your initWithID:Name: method then it is being released which is why your code isn't working. In your initWithID:Name: method at a retain when you reassign the name to your object's instance variable.

Community
  • 1
  • 1
Brian
  • 3,571
  • 7
  • 44
  • 70