Cocoa中有一类名为属性列表的对象是plist。
属性列表类包含NSArray ,NSDictionary,NSString,NSNumber ,NSDate和NSData。
看看NSDate和NSData用法。
NSDate *dates ;
dates=[NSDatedate];
NSLog(@"today is %@",dates);
NSDate *yesterday=[NSDatedateWithTimeIntervalSinceNow:-(24*60*60)];
NSLog(@"yesterday is %@",yesterday);
const char *string ="Hi there,this is c string ";
NSData *data=[NSData dataWithBytes:string length:strlen(string)+1];//+1用于包含c字符串所需的尾部零字节。
NSLog(@"data is %@",data);
NSLog(@"%d byte string is '%s'",[datalength],[databytes]);//length表示输出字节数,bytes表示指向字符串起始位置的指针。
NSArray *phrase;
phrase = [NSArrayarrayWithObjects:@"I", @"seem",@"to", @"be",@"a", @"verb",nil];
[phrasewriteToFile: @"/tmp/verbiage.txt" atomically: YES];//将属性方法类表写入文件。
NSLog (@"%@", phrase);
NSArray *phrase2 = [NSArrayarrayWithContentsOfFile: @"/tmp/verbiage.txt"];
NSLog (@"%@", phrase2);
如何找到临时文件夹tmp呢?
打开Finder,然后使用快捷键Shift+Command+G 弹出一个对话框,输入/tmp就会找到进入tmp文件夹。
plutil -convert xml1 filename.plist可以将这些文件转换成可读的形式。
该方法的缺点是不会返回任何错误信息。
编码对象 NSCoder类时一个抽象类,定义一些有用的方法来在对象于NSData之间来回转换。
如果父类采用了NSCoding协议,则应该调用[super initWithCoder:decoder],否则,只需要调用[super init]即可。
@interface Things :NSObject<NSCoding>
{
NSString *name;
int magicNumber;
float shoeSize;
NSMutableArray *subThings;
}
@property (copy)NSString *name;
@propertyint magicNumber;
@propertyfloat shoeSize;
@property (retain)NSMutableArray *subThings;
-(id)initWithName:(NSString *)n magicNumber:(int) mn shoeSize:(float) ss;
@end
@implementation Things
@synthesize name;
@synthesize magicNumber;
@synthesize shoeSize;
@synthesize subThings;
-(id)initWithName:(NSString *)n magicNumber:(int)mn shoeSize:(float)ss
{
if(self=[superinit])
{
self.name=n;
self.magicNumber=mn;
self.shoeSize=ss;
self.subThings=[NSMutableArrayarray];
}
return self;
}
-(void) dealloc
{
[namerelease];
[subThingsrelease];
[superdealloc];
}
-(void) encodeWithCoder:(NSCoder *)aCoder//方法与每个实例变量名称匹配的键下编码这些实例变量。
{
[aCoderencodeObject:nameforKey:@"name"];
[aCoderencodeInt:magicNumberforKey:@"magicNumber"];//每种类型都有不同的encodeSomething: forkey:
[aCoderencodeFloat:shoeSizeforKey:@"shoeSize"];
[aCoderencodeObject:subThingsforKey:@"subThings"];
}
-(id) initWithCoder:(NSCoder *)aDecoder//如果需要恢复某个对象 可以使用decodeSomething: forkey:
{
if(self=[superinit])
{
self.name=[aDecoder decodeObjectForKey:@"name"];
self.magicNumber=[aDecoder decodeIntForKey:@"magicNumber"];
self.shoeSize=[aDecoder decodeFloatForKey:@"shoeSize"];
self.subThings=[aDecoderdecodeObjectForKey:@"subThings"];
}
return self;
}
-(NSString *) description
{
NSString *description=[NSStringstringWithFormat:@"%@:%d/%.1f %@",name ,magicNumber,shoeSize,subThings];
return (description);
}
@end
main中主要代码:Things *thing1;
thing1=[[Thingsalloc] initWithName:@"thing1"magicNumber:42shoeSize:10.5];
NSLog(@"some thing: %@",thing1);
NSData *freezeDried;
freezeDried = [NSKeyedArchiverarchivedDataWithRootObject: thing1];//先创建一个NSKeyedArchiver实例,然后将它传递给thing1的-encodeWithCoder方法
[thing1 release];
thing1 = [NSKeyedUnarchiverunarchiveObjectWithData: freezeDried];
NSLog (@"reconstituted thing: %@", thing1);
Things *anotherThing;
anotherThing=[[Thingsalloc]initWithName:@"thing2"magicNumber:23shoeSize:13.0];
[thing1.subThingsaddObject:anotherThing];
anotherThing=[[Thingsalloc]initWithName:@"thing3"magicNumber:17shoeSize:9.0];
[thing1.subThingsaddObject:anotherThing];
NSLog(@"thing with anotherthing: %@",thing1);