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

  • SEARCH
  • Home
  • 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 882043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:21:56+00:00 2026-05-15T12:21:56+00:00

My data is stored in a member variable (NSArray) of a view controller. The

  • 0

My data is stored in a member variable (NSArray) of a view controller. The problem I’m running into is that my data is loaded from a database on application launch, but the NSArray isn’t initialized until later, so the addObject calls silently fail.

I’ve tried putting breakpoints on the init, initWithNibName, viewWillAppear, and viewDidLoad methods of my view controller (SafeTableViewController), but none of them catch before the addObject call. I assume that the actual view controller is initialized, because when I watch it in the debugger it has a nonzero address, but the NSArray has the address 0x0 when addObject is called.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    databaseName = @"DubbleDatabase.sql";

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];

    [self checkAndCreateDatabase];

    [self readSafeItemsFromDatabase ];

    // Add the tab bar controller's current view as a subview of the window
    [window addSubview:tabBarController.view];
    [window makeKeyAndVisible];

    return YES;
}
- (void) readSafeItemsFromDatabase {
    // some code skipped here, but basically: open sqlite3 database, iterate through rows

            while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
                // read database, get data fields out

                SafeItem *safeItem = [[SafeItem alloc] initWithName:aName price:aPrice category:aCategory];

                [safeTableViewController addItemToSafe: safeItem]; // PROBLEM HERE

                [safeItem release];
            }
    }
    sqlite3_close(database);
}

In SafeTableViewController.m:

- (void) addItemToSafe : (SafeItem*) newSafeItem {
    [self.safeItems addObject: newSafeItem]; 
}

// I put a breakpoint on this, but it does not hit.  i.e. safeItems is not initialized when addObject is called on it.
-(id) init {
    if(self = [super initWithNibName:@"SafeTableViewController" bundle:nil]){
        self.safeItems = [[NSMutableArray alloc] init];
    }
    return self;
}

EDIT: Thought of a way to fix this problem. Still curious though: when is init and/or initWithNibName called? Here’s the proposed solution:

- (void) addItemToSafe : (SafeItem*) newSafeItem {
    if(self.safeItems == nil){
        self.safeItems = [[NSMutableArray alloc] init];
    }
    [self.safeItems addObject: newSafeItem]; 
}
  • 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-15T12:21:56+00:00Added an answer on May 15, 2026 at 12:21 pm

    How do you setup your instance of SafeTableViewController? By code? By nib?

    -(id) init is not the designated initializer.

    You probably want to use

    - (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle {
        if(self = [super initWithNibName:nibName bundle:nibBundle]){
            self.safeItems = [[NSMutableArray alloc] init];
        }
        return self;
    }
    

    or initialize elsewhere, i.e. in viewDidLoad.

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

Sidebar

Ask A Question

Stats

  • Questions 437k
  • Answers 438k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I don't think that it would be possible to use… May 15, 2026 at 4:25 pm
  • Editorial Team
    Editorial Team added an answer .remove([selector]) will remove an element with the optional matching selector… May 15, 2026 at 4:25 pm
  • Editorial Team
    Editorial Team added an answer Are you talking about a LINQ DataContext? If so, this… May 15, 2026 at 4:25 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.