先前的博文中 我 提到过 我们 可以采取 encodeObject:forKey:这项措施 对基本的Objective-C物件 和 编写过编码措施及解码措施的物件 进行编码。如果 某类物件中的每一个 都包含 一些基本的数据类型,比如 整数类型int,浮点类型float,我们 必须知道 如何对他们 进行编码 和 解码。这个 在先前一篇博文中的表格里 已经列举出来了。
我们 创建 一类简单的物件MyClass。下面 是 MyClass这类物件的接口文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#import <Foundation/Foundation.h>
@interface MyClass : NSObject<NSCoding>
{
NSString *stringValue;
int intValue;
float floatValue;
}
@property (copy,nonatomic) NSString *stringValue;
@property int intValue;
@property float floatValue;
@end
|
接下来 是 MyClass这类物件的实施文件:
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
|
#import "MyClass.h"
@implementation
MyClass
@synthesize
stringValue,intValue,floatValue;
-(void)encodeWithCoder:(NSCoder
*)encoder
{
[encoder
encodeObject:stringValue forKey:@"MyClassStringValue"];
[encoder
encodeInt:intValue forKey:@"MyClassIntValue"];
[encoder
encodeFloat:floatValue forKey:@"MyClassFloatValue"];
}
-(id)initWithCoder:(NSCoder
*)decoder
{
stringValue=[[decoder
decodeObjectForKey:@"MyClassStringValue"] retain];
intValue=[decoder
decodeIntForKey:@"MyClassIntValue"]; 作者: cargador
抱歉!评论已关闭. 书签本站推荐 |