• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Writing objects to disk and retrieving
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Writing objects to disk and retrieving


  • Subject: Re: Writing objects to disk and retrieving
  • From: tyler <email@hidden>
  • Date: Tue, 24 Jul 2001 10:34:51 -0700

This stuff is covered pretty well in the Learning Cocoa Book examples. But presumably you don't have that handy. Here's some snippets of that code:

from the init method of the main controller:
NSString *storePath = [NSHomeDirectory() stringByAppendingPathComponent: @"Documents/TravelData.travela"];

[super init];

countryDict = [NSUnarchiver unarchiveObjectWithFile: storePath];
if ( !countryDict)
{
countryDict = [[NSMutableDictionary alloc] init];
countryKeys = [[NSMutableArray alloc] initWithCapacity: 10];
}
else
{
countryDict = [[NSMutableDictionary alloc] initWithDictionary: countryDict];

// note that if you need a mutableDict you have to create one with the contents of the NSDictionary read from the file (that's what the above line is doing).


countryKeys = [[NSMutableArray alloc] initWithArray:
[[countryDict allKeys] sortedArrayUsingSelector:
@selector(caseInsensitiveCompare:)]];
}


// then to write it out:

NSString *storePath = [NSHomeDirectory() stringByAppendingPathComponent: @"Documents/TravelData.travela"];

if ( countryDict )
[NSArchiver archiveRootObject: countryDict toFile: storePath];


You also have to make sure that all the objects in the NSMutableDictionary (or whatever collection you are using) can speak the NSCoder (NSCoding?) informal protocol.

// archiving and unarchiving

- (id)initWithCoder:(NSCoder *)aDecoder;
- (void)encodeWithCoder:(NSCoder *)aCoder;


so for the sample "Country" class they are implemented as:

// archiving and unarchiving

- (id)initWithCoder:(NSCoder *)aDecoder
{
[self setName: [aDecoder decodeObject]];
[self setAirports: [aDecoder decodeObject]];
[self setAirlines: [aDecoder decodeObject]];
[self setTransportation: [aDecoder decodeObject]];
[self setHotels: [aDecoder decodeObject]];
[self setLanguages: [aDecoder decodeObject]];
[aDecoder decodeValueOfObjCType: "s" at: &englishSpoken];
[self setCurrencyName: [aDecoder decodeObject]];
[aDecoder decodeValueOfObjCType: "f" at: &currencyRate];
[self setComments: [aDecoder decodeObject]];

return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject: [self name]];
[aCoder encodeObject: [self airports]];
[aCoder encodeObject: [self airlines]];
[aCoder encodeObject: [self transportation]];
[aCoder encodeObject: [self hotels]];
[aCoder encodeObject: [self languages]];
[aCoder encodeValueOfObjCType: "s" at: &englishSpoken];
[aCoder encodeObject: [self currencyName]];
[aCoder encodeValueOfObjCType: "f" at: &currencyRate];
[aCoder encodeObject: [self comments]];
}



Hope that helps.

peace,
tyler



On Thursday, July 5, 2001, at 10:16 AM, Mark T wrote:

The application I'm working on has to read a few files from disk, load 6 arrays with the contents of those files, and at the end of the program, it has to write the arrays back to the files to save any changes made to them. I've found methods like arrayWithContentsOfFile: for retrieving the contents from the file, but I can't find the method to create or modify a file in the first place(in a format that can be read by arrayWithContentsOfFile: ). Where might I look for such a thing?

Thank you,
Mark T.
_______________________________________________
cocoa-dev mailing list
email@hidden
http://www.lists.apple.com/mailman/listinfo/cocoa-dev


References: 
 >Writing objects to disk and retrieving (From: Mark T <email@hidden>)

  • Prev by Date: Re: Help with help
  • Next by Date: Re: CFBundleVersion questions
  • Previous by thread: Writing objects to disk and retrieving
  • Next by thread: Re: Problem with IBOutlet
  • Index(es):
    • Date
    • Thread