Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7533251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:41:01+00:00 2026-05-30T05:41:01+00:00

I have a database manager which successfully loads a string from my sqlite database,

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T05:41:02+00:00Added an answer on May 30, 2026 at 5:41 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem description: I have a POJO object, which is mapped from database. Which having
need help from some gurus here, I have a database which I have moved
I have a WCF service that uses ODP.NET to read data from an Oracle
I have a User entity which represents a User table in my database. This
I have an app which is something of a generic file manager. I would
I have a legacy database, which I am using EJB3 to model. The database
In a SQL server database, I have a table which contains a TEXT field
I have been searching for how to retrieve binary data from the database. My
I have database table: DROP TABLE translation_en_lt; CREATE TABLE translation_en_lt ( id INTEGER, lt_translation
I have Oracle 11 database to which I connect using both JDBC and ODBC.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.