Fork me on GitHub

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

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


--------------------本文结束👨‍💻感谢您的阅读--------------------
坚持原创技术分享,您的支持将鼓励我继续创作!
  • 本文标题: OC基础学习13:文件加载与保存
  • 本文作者: AndyRon
  • 发布时间: 2017年09月12日 - 21:13
  • 最后更新: 2019年09月04日 - 11:25
  • 本文链接: http://andyron.com/2017/oc-basic-13.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!
0%