Object-c学习笔记十六-----文件加载与保存
来源:互联网 发布:兰州知豆招聘信息 编辑:程序博客网 时间:2024/05/16 06:21
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);
constchar *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];
}
returnself;
}
-(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"];
}
returnself;
}
-(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);
- Object-c学习笔记十六-----文件加载与保存
- Objective-C学习笔记第十五章文件加载与保存
- Object-C 学习笔记(二十六)--- 文件断点拷贝
- 黑马程序员--Object-C文件加载与保存
- Object-C 学习笔记(十六)---Foundation之NSMutableArray
- C#学习笔记(十六):使用打开保存文件对话框
- C#学习笔记(十六):使用打开保存文件对话框
- C#学习笔记(十六):使用打开保存文件对话框
- IOS学习笔记---文件加载和保存
- Tensorflow学习笔记-模型保存与加载
- Object-C学习笔记九-----查找文件
- 文件加载与保存
- c语言学习笔记十六
- Object-C学习笔记
- object-c学习笔记
- Object-C学习笔记
- Object-c学习笔记
- Object-C学习笔记
- 忘记敏捷
- 编程之美读书笔记3.7队列中取最大值操作的问题 解法2
- 最大公共连续子串(LCS问题)
- hdu 1142 A Walk Through the Forest(spfa+深搜)
- 利用读写锁和智能指针来实现多线程下对资源的多个地方读和多个地方写的实现。
- Object-c学习笔记十六-----文件加载与保存
- Henry拣钱(money.pas/c/cpp)
- 一步一步写算法(之非递归排序)
- Linux C之atio()函数
- iPhone中的UIActionSheet与UIAlterView
- java web开发一个帐号同一时间只能一个人登录
- Creating EXT2 USB Flash Drive from Windows
- EXT2IFS performance of VHD on my Kingston UFD
- Java和Js的高精度计算