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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:48:49+00:00 2026-05-21T23:48:49+00:00

I am developing my first iphone ‘Diary’ app, which uses custom ‘Entry’ objects that

  • 0

I am developing my first iphone ‘Diary’ app, which uses custom ‘Entry’ objects that hold an NSString title, NSString text and NSDate creationDate. When I try to archive an NSMutableArray of Entry objects, and later retrieve them the next time the view loads, the app crashes. I have gone through a bunch of sample codes and examples that use NSKeyedArchivers, but still couldn’t figure out why that happens. I am guessing there is a problem with the initialization of the array that holds the entries but not sure…

Here is the code, maybe you could find something that I have persistently overseen…”

//--------- Entry.m---------------

- (id) initWithCoder:(NSCoder *)aDecoder{

if ((self = [super init])) {
    self.title = [[aDecoder decodeObjectForKey:@"title"] retain];
    self.text = [[aDecoder decodeObjectForKey:@"text"] retain];
    self.created = [[aDecoder decodeObjectForKey:@"created"] retain];
}
return self;

}

- (void) encodeWithCoder:(NSCoder *)aCoder{

[aCoder encodeObject:self.title forKey:@"title"];
[aCoder encodeObject:self.text forKey:@"text"];
[aCoder encodeObject:self.created forKey:@"created"];

}

//-------------- Diary View Controller.m

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

- (void) writeDataToArchive {
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]
                             initForWritingWithMutableData:data];

[archiver encodeObject:self.entriesArray forKey:@"entriesArray"];
[archiver finishEncoding];
BOOL result = [data writeToFile:[self dataFilePath]  atomically:YES];
[archiver release];
[data release];    
}

- (void)addItem:sender {
int count = [entriesArray count] +1;    
NSString *newEntryTitle = [NSString stringWithFormat:@"Entry %d", count];    
Entry *anEntry = [[Entry alloc] initWithTitle:newEntryTitle text:@"-" 
         created:[NSDate date]];
[entriesArray addObject:anEntry];
[self.tableView reloadData];

[anEntry release];
[self writeDataToArchive];
}

- (void)viewDidLoad
{
[super viewDidLoad];
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {     
     NSData *data = [[NSMutableData alloc]
                     initWithContentsOfFile:[self dataFilePath]];

     NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]
          initForReadingWithData:data];
     NSMutableArray *array = [unarchiver decodeObjectForKey:@"entriesArray"];     
     entriesArray = [array mutableCopy];
     [array release];
     [unarchiver finishDecoding];
     [unarchiver release];
     [data release];
}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
    (NSIndexPath *)indexPath
{
    // ... some other stuff
     NSUInteger row = indexPath.row;
     Entry *entry = [entriesArray objectAtIndex:row];

     cell.textLabel.text = entry.title;
     return cell;
}

Thanks a lot.

  • 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-21T23:48:50+00:00Added an answer on May 21, 2026 at 11:48 pm

    You should not release array in viewDidLoad because you do not own it.

    Please review the Cocoa memory management Rules because there are a couple of other memory management issues in your code. In particular,

    self.title = [[aDecoder decodeObjectForKey:@"title"] retain];
    self.text = [[aDecoder decodeObjectForKey:@"text"] retain];
    self.created = [[aDecoder decodeObjectForKey:@"created"] retain];
    

    in your initWithCoder: method all leak on the assumption the properties are retain or copy.

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

Sidebar

Related Questions

I am developing an iPhone app which uses Settings.bundle to let the user choose
I'm developing my first iPhone app, and while implementing user preferences, I have a
I've been developing my first iPhone app part-time and would like to start using
I'm developing my first iPhone app and I would greatly appreciate you guy's input
I am developing an iPhone app which is a Navigation based app. In this
I'm developing an iphone app that makes use of of a UIWebView to display
I'm developing an iPhone/iPad app that supports dragging items between table views. Since all
I have a multiview iPhone app I'm developing that will have lots of inputs
I'm developing an iPhone app at the moment using Appcelerator Titanium which has 3
first question here. I'm developing a program in C# (.NET 3.5) that displays files

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.