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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:32:24+00:00 2026-05-20T01:32:24+00:00

Read through this code. I wrote it at my desk in 5 minutes, it’s

  • 0

Read through this code. I wrote it at my desk in 5 minutes, it’s not production code but just for show. Then read my question at the bottom:

@interface SomeClass : NSObject {

NSString *currentFruit;
NSMutableArray *fruits;

}

@property (retain) NSString *currentFruit;
@property (retain) NSMutableArray *fruits;

@end

@implementation SomeClass
@synthesize currentFruit, fruits;

-(void) createArray {

    self.fruits = [[NSMutableArray alloc] initWithObjects:@"apples",@"oranges",@"grapes",nil];
    self.currentFruit = [fruits objectAtIndex:0];
}

-(void)writeToDisk {
    NSString *filePath = @"/somePath/file.ext";
    NSMutableData *fileData = [NSMutableData data];
    NSKeyedArchiver *coder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:fileData];
    [coder encodeObject:self.fruits forKey:@"fruitArray"];
    [coder encodeObject:self.currentFruit forKey:@"theFruit"];
    [coder finishEncoding];
    [fileData writeToFile:filePath atomically:YES];
    [coder release];
}

-(void)loadDataFromDisk {
    NSString *filePath = @"/somePath/file.ext";
    NSData *fileData = [[NSData alloc] initWithContentsOfFile:filePah];
    NSKeyedUnarchiver *decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:fileData];
    NSMutableArray *decodedFruits = [decoder decodeObjectForKey:@"fruitArray"];
    NSString *decodedFruit = [decoder decodeObjectForKey:@"theFruit"];
    self.currentFruit = decodedFruit;
    self.fruits = decodedFruits;
    [decoder release];
    [fileData release];
}

Say I used the method -(void)createArray to make my array and then set the instance of currentFruit to the first object in the array. Then I used -(void)writeDataToDisk and stored the objects to file. When I use loadDataFromDisk, does my application still know that currentFruit should be the object at the beginning of my fruits array? or will I have two separate instances now?

I’m trying to figure this out. Should I be keeping track of the array index instead of tracking actual objects? I want to always be referencing the object in the array. Does this make sense?

  • 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-20T01:32:24+00:00Added an answer on May 20, 2026 at 1:32 am

    EDIT: I withdraw the following answer, having somehow not realised that you’re not using NSCoder. The below applies to encoding by implementing NSCoding and supplying a single root object only. If what you want works in your case, it’ll be an implementation detail outside of the documentation and therefore not to be relied upon.

    I am inclined to believe that you’ll get the same object; the archivers detect when you write out multiple instances of the same object and use a link back to the original. Otherwise they’d be unable to write any hierarchy of objects that included a circular reference — they’d just write forever to the disk. Therefore, the information that the object ‘theFruit’ is from the array ‘fruitArray’ is encoded in the archive. See ‘Root Object’ within the Archives and Serialization Programming Guide.

    Given that you keep the same instance of NSKeyedUnarchiver, it’s not unreasonable to assume that the code underneath will be smart enough to return the same object. However, I am reading between the lines on Apple’s focus of storing an object graph and can’t find an explicit acknowledgement of this.

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

Sidebar

Related Questions

No related questions found

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.