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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:55:24+00:00 2026-06-07T08:55:24+00:00

I try to save my object to NSUserDefaults . But when I call this

  • 0

I try to save my object to NSUserDefaults. But when I call this method again it is not have any info about previous operation.

There is my method below:

- (void)addToCart {
    if([[NSUserDefaults standardUserDefaults] objectForKey:kCart]) {
        NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
        NSMutableArray *products = [[NSMutableArray alloc] initWithArray:[prefs objectForKey:kCart]];
        [products addObject:self.product];
        [prefs setObject:products forKey:kCart];
        [prefs synchronize];
        [products release];
    }
    else {
        //Saving...        
        NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
        [prefs setObject:[NSArray arrayWithObjects:self.product, nil] forKey:kCart];
        [prefs synchronize];
    }   
}

I need to save a collection with a products to NSUserDefault. I wrap my object to NSArray and save it but it doesn’t work.

  • 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-07T08:55:25+00:00Added an answer on June 7, 2026 at 8:55 am

    Everything put into NSUserDefaults must be a valid property list object (NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary). All collection elements must themselves also be property list objects.

    In order to save non-PL objects into NSUserDefaults, you must first convert the object into a PL object. The most generic way to do this is by serializing it to NSData.

    Serializing to NSData is handled with NSKeyedArchiver. See Storing NSColor in User Defaults for the canonical example of this. (That document is very old and still references NSArchiver which will work fine for this problem, but NSKeyedArchiver is now the preferred serializer.)

    In order to archive using NSKeyedArchiver, your object must conform to NSCoding as noted by @harakiri.

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

Sidebar

Related Questions

I have this error when i try to save my entity: Invalid object name
I have a problem that when I try to save an object in the
Looking on my method I'm not sure I have to use three separate try/catch
I have this error when I try to save some data to my sql
When I try to save images to a directory, it's not saving to my
When I try to save a certain of my ActiveRecord instances, I get this
this is the problem once i try to save data into db with sql
Hey when I try to save a object to internal storage. I keep on
This was my first time ever trying to save object in a file, so
I'm running into a ConstraintViolationException when I try to save an object with a

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.