美文网首页
OC基础学习13:文件加载与保存

OC基础学习13:文件加载与保存

作者: Andy_Ron | 来源:发表于2017-09-12 21:19 被阅读33次

    1 属性列表(property list, plist)

    属性列表可以保存的类型有NSArray,NSDictionary,NSString,NSNumber,NSDate,NSDate以及它们对应的Mutable类。

    • NSDate
            NSDate *date = [NSDate date];
            NSLog(@"today is %@", date);
            NSDate *yesterday = [NSDate dateWithTimeIntervalSinceNow: -(24*3600)];
            NSLog(@"yesterday is %@", yesterday);
    

    NSDateFormatter用来处理时间格式

    • NSData
            const *string = "Hi there, this is a C string!";
            NSData *data = [NSData dataWithBytes: string length: strlen(string)+1];
            NSLog(@"data is %@", data);
            // data is <48692074 68657265 2c207468 69732069 73206120 43207374 72696e67 2100>
            NSLog(@"%lu byte string is '%s'", [data length], [data bytes]);
            // 30 byte string is 'Hi there, this is a C string!'
    
    
    • 写入和读取属性列表

    写入

            NSArray *phrase;
            phrase = [NSArray arrayWithObjects:@"I", @"seem", @"to", @"be", @"a", @"verb", nil];
            [phrase writeToFile:@"/tmp/verbiage.txt" atomically:YES];
    

    verbiage.txt内容为:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <array>
        <string>I</string>
        <string>seem</string>
        <string>to</string>
        <string>be</string>
        <string>a</string>
        <string>verb</string>
    </array>
    </plist>
    

    Mac电脑系统中 ~/Library/Preferences下的所有首选项文件和/System/Library/LaunchDaemons/下的系统配置文件都是plist

    读取

            NSArray *phrase2;
            phrase2 = [NSArray arrayWithContentsOfFile:@"/tmp/verbiage.txt"];
            NSLog(@"%@", phrase2);
    

    -writeToFile:atomically:方法中的atomically表示是否首先将文件内容保存在临时文件中,再将临时文件和原始文件交换。

    • 修改对象类型
      NSPropertyListSerialization

    2 编码对象

    无法将对象信息表示为属性列表类。
    编码和解码(encoding and decoding) 和 序列化与反序列化(serialization and deserialization) 必须采用NSCoding协议
    在Interface Builder中的的对象就被序列化保存到磁盘中了。
    NSCoding定义两个方法,用于对象与NSDate之间的转换:

    @protocol NSCoding
    
    - (void)encodeWithCoder:(NSCoder *)aCoder;
    - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder; // NS_DESIGNATED_INITIALIZER
    
    @end
    

    一个列子

    @interface Thingie : NSObject <NSCoding>
    {
        NSString *name;
        int magicNumber;
        float shoeSize;
        NSMutableArray *subThingies;
    }
    
    @property (copy) NSString *name;
    @property int magicNumber;
    @property float shoeSize;
    @property (retain) NSMutableArray *subThingies;
    
    - (id)initWithName: (NSString *) n
           magicNumber: (int) mn
              shoeSize: (float) ss;
    
    @end // Thingie
    

    Thingie采用了NSCoding协议,要实现编码和解码响应的方法。

    @implementation Thingie
    @synthesize name;
    @synthesize magicNumber;
    @synthesize shoeSize;
    @synthesize subThingies;
    
    - (id)initWithName: (NSString *) n
           magicNumber: (int) mn
              shoeSize: (float) ss
    {
        if (self = [super init])
        {
            self.name = n;
            self.magicNumber = mn;
            self.shoeSize = ss;
            self.subThingies = [NSMutableArray array];
        }
        
        return (self);
    }
    
    - (void) encodeWithCoder: (NSCoder *) coder
    {
        [coder encodeObject: name
                     forKey: @"name"];
        [coder encodeInt: magicNumber
                  forKey: @"magicNumber"];
        [coder encodeFloat: shoeSize
                    forKey: @"shoeSize"];
        [coder encodeObject: subThingies
                     forKey: @"subThingies"];
        
    } // encodeWithCoder
    
    
    - (id) initWithCoder: (NSCoder *) decoder
    {
        if (self = [super init])
        {
            self.name = [decoder decodeObjectForKey: @"name"];
            self.magicNumber = [decoder decodeIntForKey: @"magicNumber"];
            self.shoeSize = [decoder decodeFloatForKey: @"shoeSize"];
            self.subThingies = [decoder decodeObjectForKey: @"subThingies"];
        }
        
        return (self);
        
    } // initWithCoder
    
    
    - (NSString *) description
    {
        NSString *description =
        [NSString stringWithFormat: @"%@: %d/%.1f %@",
         name, magicNumber, shoeSize, subThingies];
        
        return (description);
        
    } // description
    
    
    @end // Thingie
    

    相关文章

      网友评论

          本文标题:OC基础学习13:文件加载与保存

          本文链接:https://www.haomeiwen.com/subject/ayuxsxtx.html