Object-c学习笔记十六-----文件加载与保存

Object-c学习笔记十六-----文件加载与保存,第1张

概述Cocoa中有一类名为属性列表的对象是plist。 属性列表类包含NSArray ,NSDictionary,NSString,NSNumber ,NSDate和NSData。 看看NSDate和NSData用法。     NSDate *dates ;     dates=[NSDatedate];    NSLog(@"today is %@",dates);         NSDate *

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@H_403_105@用于包含c@H_403_105@字符串所需的尾部零字节。

   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 d出一个对话框,输入/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;

}

@H_301_280@@property (copy)Nsstring *name;

@propertyint magicNumber;

@H_301_280@@propertyfloat shoeSize;

@property (retain)NSMutableArray *subThings;


-(ID)initWithname:(Nsstring *)n magicNumber:(int) mn shoeSize:(float) ss;

@H_301_280@@end


@H_301_280@@implementation Things

@H_301_280@@synthesize name;

@synthesize magicNumber;

@H_301_280@@synthesize shoeSize;

@synthesize subThings;


-(ID)initWithname:(Nsstring *)n magicNumber:(int)mn shoeSize:(float)ss

{

@H_301_280@   if(self=[superinit])

    {

        self.name=n;

        self.magicNumber=mn;

        self.shoeSize=ss;

        self.subThings=[NSMutableArrayarray];

    }

@H_301_280@   return self;

}


-(voID) dealloc

{

    [namerelease];

    [subThingsrelease];

    [superdealloc];

}

-(voID) encodeWithCoder:(NSCoder *)aCoder//@H_403_105@方法与每个实例变量名称匹配的键下编码这些实例变量。

{

    [aCoderencodeObject:nameforKey:@"name"];

    [aCoderencodeInt:magicNumberforKey:@"magicNumber"];//每种类型都有不同的encodeSomething: forkey:

    [aCoderencodefloat:shoeSizeforKey:@"shoeSize"];

    [aCoderencodeObject:subThingsforKey:@"subThings"];    

}


-(ID) initWithCoder:(NSCoder *)aDecoder//如果需要恢复某个对象 可以使用decodeSomething: forkey:

{

@H_301_280@   if(self=[superinit])

       {

           self.name=[aDecoder decodeObjectForKey:@"name"];

           self.magicNumber=[aDecoder decodeIntForKey:@"magicNumber"];

           self.shoeSize=[aDecoder decodefloatForKey:@"shoeSize"];

          self.subThings=[aDecoderdecodeObjectForKey:@"subThings"];

       }

@H_301_280@   return self;

}


-(Nsstring *) description

{

   Nsstring *description=[NsstringstringWithFormat:@"%@:%d/%.1f %@",name,magicNumber,shoeSize,subThings];

    return (description);

}

@H_301_280@@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);

Cocoa提供两种方式加载保存文件。一种是属性类表,另外一种是采用NSCoding协议和实现方法来编码和解码对象。将大量对象转换成NSData类,然后保存到磁盘中。 总结

以上是内存溢出为你收集整理的Object-c学习笔记十六-----文件加载与保存全部内容,希望文章能够帮你解决Object-c学习笔记十六-----文件加载与保存所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1064902.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存