Re: plists, core foundation and cocoa
Re: plists, core foundation and cocoa
- Subject: Re: plists, core foundation and cocoa
- From: Izidor Jerebic <email@hidden>
- Date: Sat, 2 Jun 2001 12:05:53 +0200
On Saturday, June 2, 2001, at 07:15 AM, Deirdre Saoirse Moen wrote:
Dumb newbie questions:
I'd like to read a XML file in plist form and turn it into Cocoa objects,
and vice-versa. Is that what NSSerializer does? If not, how can I do
this?
Along those lines, are the Cocoa "property list" objects related to the
CF property list objects? Is Cocoa Foundation built on top of Core
Foundation?
OK, OK, I promised to post my example at WWDC and I hadn't. I've been
busy. :)
See my pListArchiving protocol and the example to show how to read/write
from an XML file. I didn't implement all data types I'd intended and I
meant to clean up the example a bit, but it does answer your question.
http://deirdre.net/SillyFile.tar.gz
Use it however you like except to tar and feather. ;)
If you want Objective-C and Foundation (the above example is using
CoreFoundation - useful in pure C) you can proceed like code below. My
silly (:-) code uses hardcoded constants for everything, but it shows the
principle. The example creates a file /tmp/a which is a property list in
XML format, and then reads it into immutable dictionary. No error checking
etc... But it does compile and work on my machine :-)
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSMutableDictionary *d ;
NSDictionary *d2 ;
// create
d = [NSMutableDictionary dictionary] ;
[d setObject:@"stringValue" forKey:@"key string"] ;
[d setObject:[NSNumber numberWithInt:10] forKey:@"key int"] ;
[d writeToFile:@"/tmp/a" atomically:YES] ;
// read
d2 = [[NSString stringWithContentsOfFile:@"/tmp/a"] propertyList] ;
NSLog( @"d2 = %@", d2 ) ;
[pool release];
return 0;
}