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 3979164
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:08:10+00:00 2026-05-20T05:08:10+00:00

I have used this code to load data from a plist. -(void)loadOrCreateData { NSLog(@Start

  • 0

I have used this code to load data from a plist.

-(void)loadOrCreateData {
    NSLog(@"Start loadOrCreateData");
    NSString *filePath = [self dataFilePath];
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) 
    {
        NSLog(@"File Exists.. Loading from plist File");
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
        font = [array objectAtIndex:0];
        background = (NSString *)[array objectAtIndex:1];
        animation = [array objectAtIndex:5];
        [array release];
        NSLog(@"Loading Done!");
    }
    else 
    {
        NSLog(@"File does not exist.. Creating new plist File");
        font = @"Georgia-BoldItalic";
        background = @"monalisa.jpeg";
        animation = @"103";
        [self saveData];
    }

    NSLog(@"Finish loadOrCreateData");
}

- (NSString *)dataFilePath {
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *documentsDirectory = [paths objectAtIndex:0];
   return [documentsDirectory stringByAppendingPathComponent:@"data.plist"];
}

- (void)saveData {
   NSMutableArray *array = [[NSMutableArray alloc] init];
   [array addObject:font];
   [array addObject:background];
   [array addObject:animation];
   [array writeToFile:[self dataFilePath] atomically:YES];
   [array release];
}

The first time everything loads fine when there is no plist file to get data from. But on second load, the app crashes when I try to use the loaded data outside the loadOrCreate method. For some reason, the data in font, background and animation is not available when accessed outside loadOrCreate method. The variables –
font, background and animation are declared as NSStrings in the .h file and therefore should be globally available right? Can you please tell me what the reason for this is?

  • 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-20T05:08:11+00:00Added an answer on May 20, 2026 at 5:08 am

    You have to retain that objects.

        font = [[array objectAtIndex:0] retain];
        background = (NSString *)[[array objectAtIndex:1] retain];
        animation = [[array objectAtIndex:5] retain];
    ...
        font = [@"Georgia-BoldItalic" retain];
        background = [@"monalisa.jpeg" retain];
        animation = [@"103" retain];
    

    Note: if you’re loading data several times, then don’t forget to release values before you set them.

    Edit:

    -(void)loadOrCreateData {
        [font release];
        [background release];
        [animation release];
    
        NSLog(@"Start loadOrCreateData");
        NSString *filePath = [self dataFilePath];
        if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) 
        {
            NSLog(@"File Exists.. Loading from plist File");
            NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
            font = [[array objectAtIndex:0] retain];
            background = (NSString *)[[array objectAtIndex:1] retain];
            animation = [[array objectAtIndex:5] retain];
            [array release];
            NSLog(@"Loading Done!");
        }
        else 
        {
            NSLog(@"File does not exist.. Creating new plist File");
            font = [@"Georgia-BoldItalic" retain];
            background = [@"monalisa.jpeg" retain];
            animation = [@"103" retain];
            [self saveData];
        }
    
        NSLog(@"Finish loadOrCreateData");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have used this code for extracting urls from web page.But in the line
I have used this code (kind of tutorial) at http://code.google.com/p/gwt-examples/wiki/gwt_hmtl5 ... In this code,
I have used this code to store Object to a file: try{ FileOutputStream saveFile=new
I am creating a rails app and have used this code in one of
the xml <publication_date media_type=print> <month>1</month> <year>2011</year> </publication_date> I have used this code to get
I have used this custom grid view in my code, and I want to
I've used this code to have a like btn on my site: <iframe src=http://www.facebook.com/plugins/like.php?href=URL_OF_YOUR_WEBSITE&amp;send=false&amp;layout=standard&amp;width=450&amp;show_faces=true&amp;action=like&amp;colorscheme=light&amp;font&amp;height=80&amp
I have to move a file in the system32 folder, I used this code:
I have used code like this: http://msdn.microsoft.com/en-us/library/dw70f090.aspx to access database before when working in
I used to have this code working with my Tomcat server: HttpRequestBase targetRequest =

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.