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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:54:02+00:00 2026-05-11T10:54:02+00:00

I have a custom object that simply inherits from NSObject. It has 3 members

  • 0

I have a custom object that simply inherits from NSObject. It has 3 members – two floats and an NSDate.

My app is going to have an array with a number of these objects kicking around, and I need to persist it between runs. How can I accomplish this?

I’ve thought about using a SQLite db, but I’m thinking that it’s a bit overkill since the only query I’d ever do would be select *.

In an ideal world I’d like to use an xml plist file. I’m not sure if I can do this with my custom object though. I know there’s a set of Property List objects, and that NSArray comes under that, but writeToFile:atomically: only works with property list objects.

Any thoughts would be appreciated, thanks!

  • 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. 2026-05-11T10:54:03+00:00Added an answer on May 11, 2026 at 10:54 am

    NSCoding will do exactly what you want. I recommend you read up on it in the Apple docs, but I thought it was pretty straightforward to use. Your class (and any child classes) will need to implement the NSCoding protocol and you’ll need to add -encodeWithCoder: and -initWithCoder: methods to your objects. Most of the common framework classes implement NSCoding already.

    The code for your class will look something like this:

    -(void) encodeWithCoder: (NSCoder*) coder {   [coder encodeInteger: versionValue forKey: versionKey];   [coder encodeObject: myStuff forKey: myStuffKey]; }  -(id) initWithCoder: (NSCoder*) coder {   self = [super init];   if ( ! self) return nil;   myStuff = [[coder decodeObjectForKey: myStuffKey] retain];   return self; } 

    It’s recommended you add a version number when encoding to give you flexibility to manage changes to your archive format in future versions.

    In my class, I added a convenience method to archive my object:

    -(void) archiveToFile: (NSString*) path {   NSMutableData *data = [[NSMutableData alloc] init];   NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData: data];   [archiver encodeObject: self forKey: myArchiveKey];   [archiver finishEncoding];   [archiver release];   [data writeToFile: path atomically: YES];   [data release]; } 

    and another one to unarchive or create a new object:

    +(MyArchive*) newFromFile: (NSString*) path             orWithMyStuff: (MyStuff*) myStuff {   NSData *data = [[NSData alloc] initWithContentsOfFile: path];   NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData: data];   MyArchive *myArchive = [unarchiver decodeObjectForKey: myArchiveKey];   [unarchiver finishDecoding];    if (myArchive) {     [myArchive retain];   } else {     myArchive = [[MyArchive alloc] initWithStuff: myStuff;   }   [unarchiver release];   [data release];   return myArchive; } 

    Since your top level object is an NSArray, you’ll need to modify the last two methods for your case, but most of the boilerplate code will be the same.

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

Sidebar

Related Questions

I made a custom object that inherits from NSObject. Let's say I have an
I have a UITableView that displays various nsstrings from a custom object called a
I have a custom control Workspace that inherits from Control and within it is
I have a custom AuthenticationProvider that simply returns the Authentication object for the authenticate
I have a custom object that I was adding to an array via a
I have a custom principal object that I want to be able to serialize
i have an ASP.NET web service that returning a custom entity object (Staff): [WebMethod]
I have a data object -- a custom class called Notification -- that exposes
I've made a custom control that inherits from a Literal control. When I try
I have a custom built web service that is built to simply add items

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.