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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:53:28+00:00 2026-06-13T15:53:28+00:00

I have a class Notification that implements the NSCoding protocol. I have an array

  • 0

I have a class Notification that implements the NSCoding protocol.
I have an array of notifications.I am trying to save the notifications with NSUserDefaults.In my app delegate notifications is a NSMutableArray that contains the Notification objects.That’s my app delegate:

+ (void) initialize
{
    NSUserDefaults* defaults=[NSUserDefaults standardUserDefaults];
    [defaults registerDefaults: [NSDictionary dictionaryWithObject: [NSKeyedArchiver archivedDataWithRootObject: [NSArray array]] forKey: @"notificationsData"]];
}


- (id) init
{
    self=[super init];
    if(self)
    {
        NSUserDefaults* defaults=[NSUserDefaults standardUserDefaults];
        NSData* notificationsData=[defaults objectForKey: @"notificationsData"];
        notifications= [[NSKeyedUnarchiver unarchiveObjectWithData: notificationsData]mutableCopy];
    }
    return self;
}

- (void) applicationWillTerminate:(NSNotification *)notification
{
    NSUserDefaults* defaults=[NSUserDefaults standardUserDefaults];
    NSData* notificationsData=[NSKeyedArchiver archivedDataWithRootObject: notifications];
    [defaults setObject: notificationsData forKey: @"notificationsData"];
}

In the Notification class text and title are of type NSString (both readwrite), and date is of type NSDate (also this has readwrite property).This is how I implement the NSCoding protocol:

- (void) encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject: date forKey: @"date"];
    [aCoder encodeObject: title forKey: @"title"];
    [aCoder encodeObject: text forKey: @"text"];
}

- (id) initWithCoder:(NSCoder *)aDecoder
{
    self=[super init];
    if(self)
    {
        date=[aDecoder decodeObjectForKey: @"data"];
        title=[aDecoder decodeObjectForKey: @"title"];
        text=[aDecoder decodeObjectForKey: @"text"];
    }
    return self;
}

So I have these problems:

  1. When the application terminates I get EXC_BAD_ACCESS in the
    Notification class, when I try to encode text with NSKeyedArchiver;
  2. The notifications aren’t saved and the array is always long zero
    when the application starts.

Update: With more debug I discovered where the application crashes.There is more code to see (I’m using a table view to display the data):

- (NSInteger) numberOfRowsInTableView:(NSTableView *)tableView
{
    return [notifications count];
}

- (id) tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    // Debug
    id obj=[[notifications objectAtIndex: row] valueForKey: [tableColumn identifier]];
    Class class=[obj class];
    // What you see above is just for debug purposes
    return [[notifications objectAtIndex: row] valueForKey: [tableColumn identifier]];
}

- (void) tableViewSelectionDidChange:(NSNotification *)notification
{
    NSInteger row=[tableView selectedRow];
    if(row >= 0 && row< [notifications count])
        [removeButton setEnabled: YES];
    else
        [removeButton setEnabled: NO];
}

The last method called is this:

- (id) tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row;

Probably the problem is that the data is somehow corrupted and the value returned by this method is not valid.Anyway the app doesn’t crash in this method, but after this method.
If I load two objects from user defaults, only one object gets loaded before crashing (so the method gets called once).
However I’m still unable to get the real reason of the crash.

More code:

- (IBAction) addNotification :(id)sender
{
    Notification* notification=[[Notification alloc]init];
    [notification setDate: [datePicker dateValue]];
    [notification setText: [textView string]];
    [notifications addObject: notification];
    [tableView reloadData];
}

- (IBAction)removeNotification:(id)sender
{
    [notifications removeObjectAtIndex: [tableView selectedRow]];
    [tableView reloadData];
}

addNotification and removeNotification are both triggered by buttons.

EDIT: I discovered that I wasn’t using ARC, but even if I turn it on the app crashes.

  • 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-06-13T15:53:29+00:00Added an answer on June 13, 2026 at 3:53 pm

    You might need to call [NSUserDefaults synchronize] since it will not happen automatically when the application suddenly exits:

    - (void) applicationWillTerminate:(NSNotification *)notification
    {
        NSUserDefaults* defaults=[NSUserDefaults standardUserDefaults];
        NSData* notificationsData=[NSKeyedArchiver archivedDataWithRootObject: notifications];
        [defaults setObject: notificationsData forKey: @"notificationsData"];
        [defaults synchronize];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data object -- a custom class called Notification -- that exposes
I have a number of classes that register with a notification class in my
I have create a class MyConnection that extends NSURLConnection. I have implemented some delegate
I have written the following class which implements(or tries to!) a dictionary with notifications:
I have class that extend FragmentActivity in it I add fragment to layout as
I have class grades that I need to check if a specific grade is
I have a class that does some generic code for Writing to a Database.
in my Silverlight 4 application, I have a class myClass that contains a list
I have an class, let's refer to it as SomeClass. SomeClass implements INotifyPropertyChanged and
So in my app I'm looking to have the students class schedules and the

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.