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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:25:47+00:00 2026-05-17T18:25:47+00:00

I Have a small problem with NSCoding and NSKeyedArchiver when persisting my App Settings

  • 0

I Have a small problem with NSCoding and NSKeyedArchiver when persisting my App Settings class and was hoping someone could spot an issue if it’s my code, I am relatively new to Obj-C but have plenty of coding experience in numerous languages so the code seems ok to me but…

I have an instance class to hold my app Settings, the class itself is a retained property of the main App Delegate, this is created via a

@interface AppDelegate : NSObject <UIApplicationDelegate> {
    Settings *settings;
    [...] 
}

@property (nonatomic, retain) Settings *settings;
[...]

This is created in the applicationDidFinishLaunching event as:

settings = [Settings LoadSettings];

If I comment out the above line then the app works fine every time, however if I pull the oject back from persisted settings using NSCoder and NSKeyedUnarchiver, the SIGARBT error is thrown as a NSCFString selector is being sent for what is encoded as a boolean property? The Settings Class is defined as an NSObject which implements the protocol.

@interface Settings : NSObject <NSCoding>

As I say, creating an instance of the settings class is fine, there is no issue, saving it seems OK as well as checking the returning class from the LoadSettings method shows the right values, only after exiting the method does the expected bool value seem to be getting sent to the load method as an NSCFString

- (id)initWithCoder:(NSCoder *)decoder {
    if (self = [super init]) {
            [...]
        self.animateMenus = [decoder decodeBoolForKey:@"animateMenus"];
    }

    return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder {
    [...]
    [encoder encodeBool:animateMenus forKey:@"animateMenus"];
}

Once the settings have been loaded the property in question is used like this:

SettingsViewController *settingsView = [[SettingsViewController alloc] initWithNibName:@"SettingsView" bundle:nil];
[self presentModalViewController:settingsView animated:[AppDelegate instance].settings.animateMenus];
[settingsView release];

**The animateMenus member of the settings class will now throw the following:

-[NSCFString animateMenus]: unrecognized selector sent to instance 0xc712570 2010-10-15 11:12:51.828 App[900:207] * Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[NSCFString animateMenus]: unrecognized selector sent to instance 0xc712570’

Whereas, taking the ‘settings = [Settings LoadSettings];’ call out of the app start-up removes the issue (but then always uses the app defaults)?

Load and Save Methods:

+ (Settings*) LoadSettings { 
    Settings *s = nil;

    @try {
        NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"settings"];

        if (data == nil) {
            s = [[Settings alloc] init];
            [s Initialise];
            [s SaveSettings];
        }
        else
            s = (Settings*)[NSKeyedUnarchiver unarchiveObjectWithData:data];
    }
    @catch (NSException * e) {
        NSLog(@"Error Loading Settings\n%@", [e reason]);
    }
    @finally {
        return s;
    }
}

// Saves the settings dictionary to the user's device documents folder..
- (void) SaveSettings { 
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self];
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"settings"];
}

Load is a static method all other members of Settings are instance.

  • 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-17T18:25:48+00:00Added an answer on May 17, 2026 at 6:25 pm

    You need to do:

    settings = [[Settings LoadSettings] retain];
    

    That’s because in your LoadSettings, the result of NSKeyedUnarchiver is an autoreleased object. And it got released 🙂 At that location a new object was created, an NSString in this case.

    Edit:

    Well, I just noticed a major problem with LoadSettings that I missed at first: you mix memory release strategies: in one codepath you return the result of [[Settings alloc] init] which is not autoreleased, while in the other you retain the result of NSKeyedArchiver which is autoreleased. You need to make sure only one concept is used.

    Since the method name LoadSettings does not contain alloc, copy or new in its name the convention is that it should return an autoreleased object. Thus, you should do:

        if (data == nil) {
            s = [[Settings alloc] init];
            [s Initialise];
            [s SaveSettings];
            [s autorelease];
        }
        else
            ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small problem with interfaces. Here it is in Pseudo code :
I have small problem with my .net 2.0 winforms application. I want to embed
I have a small problem with a script. I want to have a default
I have a small problem with how should i think a... problem. I want
SQL to find duplicate entries (within a group) I have a small problem and
I have a small math problem I am trying to solve Given a number
I'm having a small problem in Java. I have an interface called Modifiable. Objects
i have small problem i.e. their is one celltable with lot of data it
I am newbe to php. I have small problem to compare dates in php.
I have a small problem. I am trying to check to see if status's

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.