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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:15:49+00:00 2026-05-28T07:15:49+00:00

I have tons of objects which I want to save for offline use. Currently

  • 0

I have tons of objects which I want to save for offline use.
Currently I use create NSCoder compliant classes for the objects and coded data to file to be available offline.

So in the .h I introduce the objects:

@interface MyClass : NSObject<NSCoding>{
NSNumber* myObject;}
@property(nonatomic,retain) NSNumber* myObject;

And in .m I make the inits:

- (id) initWithCoder: (NSCoder *)coder {
   if (self = [super init]) {
        [self setMyObject: [coder decodeObjectForKey:@"myObject"]];
   }
}

- (void) encodeWithCoder: (NSCoder *)coder { 
    [coder encodeObject: myObject forKey:@"myObject"];
}

So the class is just dummy storage with getter and setter.
Is here any better way to do the decode / encode.
Can I use somehow @dynamic or Key-value coding for encode and decode?
Basically I want all the variables in class saved to file and back to object when program starts up.
This approach work, but creating all classes takes time and effort.

  • 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-28T07:15:49+00:00Added an answer on May 28, 2026 at 7:15 am

    Yes, you can do this automatically. First import these into your class:

    #import <objc/runtime.h> 
    #import <objc/message.h>
    

    Now add this method, which will use low-level methods to get the property names:

    - (NSArray *)propertyKeys
    {
        NSMutableArray *array = [NSMutableArray array];
        Class class = [self class];
        while (class != [NSObject class])
        {
            unsigned int propertyCount;
            objc_property_t *properties = class_copyPropertyList(class, &propertyCount);
            for (int i = 0; i < propertyCount; i++)
            {
                //get property
                objc_property_t property = properties[i];
                const char *propertyName = property_getName(property);
                NSString *key = [NSString stringWithCString:propertyName encoding:NSUTF8StringEncoding];
    
                //check if read-only
                BOOL readonly = NO;
                const char *attributes = property_getAttributes(property);
                NSString *encoding = [NSString stringWithCString:attributes encoding:NSUTF8StringEncoding];
                if ([[encoding componentsSeparatedByString:@","] containsObject:@"R"])
                {
                    readonly = YES;
    
                    //see if there is a backing ivar with a KVC-compliant name
                    NSRange iVarRange = [encoding rangeOfString:@",V"];
                    if (iVarRange.location != NSNotFound)
                    {
                        NSString *iVarName = [encoding substringFromIndex:iVarRange.location + 2];
                        if ([iVarName isEqualToString:key] ||
                            [iVarName isEqualToString:[@"_" stringByAppendingString:key]])
                        {
                            //setValue:forKey: will still work
                            readonly = NO;
                        }
                    }
                }
    
                if (!readonly)
                {
                    //exclude read-only properties
                    [array addObject:key];
                }
            }
            free(properties);
            class = [class superclass];
        }
        return array;
    }
    

    Then here are your NSCoder methods:

    - (id)initWithCoder:(NSCoder *)aDecoder
    {
        if ((self = [self init]))
        {
            for (NSString *key in [self propertyKeys])
            {
                id value = [aDecoder decodeObjectForKey:key];
                [self setValue:value forKey:key];
            }
        }
        return self;
    }
    
    - (void)encodeWithCoder:(NSCoder *)aCoder
    {
        for (NSString *key in [self propertyKeys])
        {
            id value = [self valueForKey:key];
            [aCoder encodeObject:value forKey:key];
        }
    }
    

    You have to be a bit careful with this. There are the following caveats:

    1. This will work for properties that are numbers, bools, objects, etc, but custom structs won’t work. Also, if any of the properties in your class are objects that don’t themeselves support NSCoding, this won’t work.

    2. This will only work with synthesized properties, not ivars.

    You could add error handling by checking the type of a value in encodeWithCoder before encoding it, or overriding the setValueForUndefinedKey method to handle a problem more gracefully.

    UPDATE:

    I’ve wrapped these methods up into a library: https://github.com/nicklockwood/AutoCoding – the library implements these methods as a category on NSObject so any class can be saved or loaded, and it also adds support for coding inherited properties, which my original answer doesn’t handle.

    UPDATE 2:

    I’ve updated the answer to correctly deal with inherited and read-only properties.

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

Sidebar

Related Questions

I have tons of objects of the same class obj , currently defined as
i have tons of data that i would like to highlight only those cell
I have tons of data in directory called reports. While doing git merge with
I am currently designing a data structure in which I'm trying to keep memory
I have tons of ajax components on this booking engine. I need to customize
Let's say I have int a() { /* Tons of code ....*/ return someInt;
Guys, I HAVE tried reading tons of stuff about EJB. And I don't get
I have a web application that uses TONS of javascript, and as usual, there
I have a JS script with tons of functions, on big on is this:
I have a web site already developed with tons of updatePanels, buttons, and integrated

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.