1 属性列表(property list, plist)
属性列表可以保存的类型有NSArray
,NSDictionary
,NSString
,NSNumber
,NSDate
,NSDate
以及它们对应的Mutable类。
NSDate
1 | NSDate *date = [NSDate date]; |
NSDateFormatter
用来处理时间格式
NSData
1 | const *string = "Hi there, this is a C string!"; |
- 写入和读取属性列表
写入1
2
3NSArray *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"1.0" encoding="UTF-8" xml version=
<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
3NSArray *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 | @protocol NSCoding |
一个列子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