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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:56:22+00:00 2026-06-06T19:56:22+00:00

I save a custom object in NSUserDefults the object has more than one property

  • 0

I save a custom object in NSUserDefults the object has more than one property ,
i initialize the value of the custom object in the AppDelegate ,
my problem is that when i try to retrieve the custom object property “userToken” in the viewcontroller didLoad method it works and the data is correct but when i retrieve it in another method in the view controller i get NULL note that the other property “uId” returns the correct value and i’m retrieving the objects in same way

what did i do wrong here ? help is really appreciated

here’s how i save and retrieve the object

+(void)SaveUser:(User *)user
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSData *userData = [NSKeyedArchiver archivedDataWithRootObject:user];
    [userDefaults setObject:userData forKey:@"User"];  
    [userDefaults synchronize]; 
}

+(User*)retrieveUser
{
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    NSData *userData = [ud objectForKey:@"User"];
    User *user = [NSKeyedUnarchiver unarchiveObjectWithData:userData];
    return user;    
}

and here’s the implementation of the User object

- (id)initWithCoder:(NSCoder *)decoder 
{
   self = [super init];
   if (self)
   {    
        //uId and userToken are of type NSString
        self.uId = [decoder decodeObjectForKey:@"UID"];
        self.userToken = [decoder decodeObjectForKey:@"DeviceToken"];
   }
   return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder 
{
    [encoder encodeObject:self.uId forKey:@"UID"];
    [encoder encodeObject:self.userToken forKey:@"DeviceToken"];
}
  • 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-06T19:56:24+00:00Added an answer on June 6, 2026 at 7:56 pm

    Try this :
    Also note that + mean as static function that didn’t have access to class properties make every thing in your saveuser or retrieveuser functions also decode without call another method out of class to decode it . that why maybe your + method get always null value when you use it out of loadview

    +(void)SaveUser:(User *)user
    {
        NSUserDefaults *UserDefaults = [NSUserDefaults standardUserDefaults];
        [UserDefaults setObject:[NSKeyedArchiver archivedDataWithRootObject:user] forKey:@"User"];
        [UserDefaults synchronize];
    }
    
    +(User*)retrieveUser
    {
        NSUserDefaults *UserDefaults = [NSUserDefaults standardUserDefaults];
        NSData *userData = [UserDefaults objectForKey:@"User"];
        User *user = [NSKeyedUnarchiver unarchiveObjectWithData:userData];
        return user;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom slider-type object, that I wish to make more usable. Currently
I am trying to save a custom object as a user setting in a
In some book I've seen that they save custom properties of user control like
What other ways than NSUserDefaults are there to save and get back Custom Objects?
I am using EF4 CTP5 to try to persist a POCO object that is
I have a custom class that I wish to save and load. The class
Relevant code: JToolBarHelper::custom('saveCategories', 'save', '', 'Save', false, false); ... <input type=hidden name=controller value=EasyBlogController> According
I want to save some custom data into application configuration file and I need
i am using custom checkbox buttons in my table view and want to save
Here is a portion of my struts.xml: <constant name=struts.custom.i18n.resources value=global /> and <action name=picture_save

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.