1 属性列表(property list, plist)

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

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

NSDateFormatter用来处理时间格式

  • NSData
1
2
3
4
5
6
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!'
  • 写入和读取属性列表

写入

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

verbiage.txt内容为:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<?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

读取

1
2
3
        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之间的转换:

1
2
3
4
5
6
@protocol NSCoding

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

@end

一个列子

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
@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协议,要实现编码和解码响应的方法。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
@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