• 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: Saving data to a file
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Saving data to a file


  • Subject: RE: Saving data to a file
  • From: "Mondragon, Ian" <email@hidden>
  • Date: Fri, 14 Mar 2003 16:36:32 -0600

ron,

if you'd like a human readable archive for an object, here's what i tend to
do with objects:

1. implement a -dictionaryRepresentation method that spits out a dictionary
containing all pertinent information about the object
2. implement an -initFromDictionary: method that allows you to initialize an
object from a dictionary (that you'd read in from such an archive)
3. wrap these methods into read/write methods for files.
4. use the standard NSDictionary methods & go nuts

below is a really quick example WITHOUT ERROR CHECKING/accessor
methods/etc., but it should give you an idea. the nice thing about doing
things this way (i.e. using the standard foundation objects) is that it's
VERY extensible. your milage may vary, though.

for non-human-readable archives, check out the "Archiving and Serialization"
section in PB's Cocoa help.

hope this gets you going...

- ian

--- snip ---

@interface SaveThisObject : NSObject
{
NSString *name;
NSArray *array;
}

- initFromDictionary:(NSDictionary *)aDictionary;
- initFromFile:(NSString *)filename;

- (NSDictionary *)dictionaryRepresentation;
- (BOOL)saveToFile:(NSString *)filename;

// other methods, of course, go here

@end

--- snip ---

NSString *_NameString = @"name";
NSString *_ArrayString = @"array";

@implementation SaveThisObject

- initFromDictionary:(NSDictionary *)aDictionary
{
if ((self = [super init]))
{
// PLEASE put error checking here - this is just a quicky
[self setName:[aDictionary objectForKey:_NameString]];
[self setArray:[aDictionary objectForKey:_ArrayString]];

return self;
}

return nil;
}

- initFromFile:(NSString *)filename
{
return [self initFromDictionary:[NSDictionary
dictionaryWithContentsOfFile:filename]];
}

- (NSDictionary *)dictionaryRepresentation
{
NSMutableDictionary *dict = [NSMutableDictionary dictionary];

[dict setObject:name forKey:_NameString];
[dict setObject:array forKey:_ArrayString];

return dict;
}

- (BOOL)saveToFile:(NSString *)filename
{
return [[self dictionaryRepresentation] writeToFile:filename
atomically:YES];
}

@end

--- snip ---

> -----Original Message-----
> From: Ron Phillips [SMTP:email@hidden]
> Sent: Friday, March 14, 2003 3:48 PM
> To: email@hidden
> Subject: Saving data to a file
>
> I have scoured the cocoa-dev archives, web sites and books and still am
> confused on how best to write data to a file from a non-document-based
> application. An XML format is fine, human-readable is not required. I
> have a collection of arrays, ints, strings, and BOOLs. What
> preparation is needed for a property list? Chris Kane indicated that
> archiving is not compatible with plists and writeToFile:atomically:.
> That's cool, so do I just *somehow* wrap up my high-level object into
> an NSData object and do the write? If so, what's the best approach to
> do that? Is encoding still needed?
>
> Thanks in advance and best regards,
> Ron
> _______________________________________________
> cocoa-dev mailing list | email@hidden
> Help/Unsubscribe/Archives:
> http://www.lists.apple.com/mailman/listinfo/cocoa-dev
> Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Prev by Date: RE: Saving data to a file
  • Next by Date: Sheet vs Window updating
  • Previous by thread: Re: Saving data to a file
  • Next by thread: Re: Saving data to a file
  • Index(es):
    • Date
    • Thread