Qustion:(MRC下)

1、定义一个Computer类

实例变量:float width; NSString *name;

方法:一个带两个参数的初始化函数;print()函数;dealloc()函数;

2、定义一个Person类

实例变量:NSString *name; Computer *c; int age;

方法:一个带三个参数的初始化函数;print()函数;dealloc()函数;

要求:用该类创建出来的对象能实现copy和存入文件。

3、主函数要求:

a)分别创建Computer类的对象cc和Person类对象pp;并输出对象信息。

b)利用copy函数创建Person类对象p1; 并输出对象信息

c)把pp存入文件hello.txt中,文件目录为Documents目录。

d)从文件hello.txt中读取对象p2, 并输出对象信息。

e)不能出现内存泄漏和多次删除。

---------------------------------------------------------------------------------------------------------

Source code:

  1 #import <Foundation/Foundation.h>
  2 
  3 @interface Computer : NSObject <NSCoding>
  4 
  5 @property (nonatomic, assign) float width;
  6 @property (nonatomic, retain) NSString *name;
  7 
  8 - (id)initWithWidth:(float)w andName:(NSString *)n;
  9 
 10 - (void)print;
 11 
 12 @end
 13 
 14 #import "Computer.h"
 15 
 16 @implementation Computer
 17 
 18 - (id)initWithWidth:(float)w andName:(NSString *)n
 19 {
 20     if (self = [super init])
 21     {
 22         _width = w;
 23         self.name = n;
 24     }
 25     return self;
 26 }
 27 
 28 - (void)print
 29 {
 30     NSLog(@"Width = %f, Name = %@", _width, _name);
 31 }
 32 //编码
 33 - (void)encodeWithCoder:(NSCoder *)aCoder
 34 {
 35     [aCoder encodeObject:[NSNumber numberWithFloat:_width] forKey:@"1"];
 36     [aCoder encodeObject:_name forKey:@"2"];
 37 }
 38 //解码
 39 - (id)initWithCoder:(NSCoder *)aDecoder
 40 {
 41     if (self = [super init])
 42     {
 43         _width = [[aDecoder decodeObjectForKey:@"1"] floatValue];
 44         self.name = [aDecoder decodeObjectForKey:@"2"];
 45     }
 46     return self;
 47 }
 48 
 49 - (void)dealloc
 50 {
 51     self.name = nil;
 52     [super dealloc];
 53 }
 54 
 55 @end
 56 
 57 #import <Foundation/Foundation.h>
 58 
 59 @class Computer;
 60 @interface Person : NSObject <NSCoding, NSCopying>
 61 
 62 @property (nonatomic, assign) int age;
 63 @property (nonatomic, retain) NSString *name;
 64 @property (nonatomic, retain) Computer *c;
 65 
 66 - (id)initWithAge:(int)a andName:(NSString *)n andComputer:(Computer *)cc;
 67 
 68 - (void)print;
 69 
 70 @end
 71 
 72 #import "Person.h"
 73 #import "Computer.h"
 74 
 75 @implementation Person
 76 
 77 - (id)copyWithZone:(NSZone *)zone
 78 {
 79     Person *p = [[Person allocWithZone:zone] initWithAge:_age andName:_name andComputer:_c];
 80     return p;
 81 }
 82 
 83 - (void)encodeWithCoder:(NSCoder *)aCoder
 84 {
 85     [aCoder encodeObject:_name forKey:@"1"];
 86     [aCoder encodeObject:_c forKey:@"2"];
 87     [aCoder encodeObject:[NSNumber numberWithInt:_age] forKey:@"3"];
 88 }
 89 
 90 - (id)initWithCoder:(NSCoder *)aDecoder
 91 {
 92     if (self = [super init])
 93     {
 94         self.name = [aDecoder decodeObjectForKey:@"1"];
 95         self.c = [aDecoder decodeObjectForKey:@"2"];
 96         _age = [[aDecoder decodeObjectForKey:@"3"] intValue];
 97     }
 98     return self;
 99 }
100 
101 - (id)initWithAge:(int)a andName:(NSString *)n andComputer:(Computer *)cc
102 {
103     if (self = [super init])
104     {
105         _age = a;
106         self.name = n;
107         self.c = cc;
108     }
109     return self;
110 }
111 
112 - (void)print
113 {
114     NSLog(@"Age = %d, Name = %@, Computer = %@", _age, _name, _c);
115 }
116 
117 - (void)dealloc
118 {
119     self.name = nil;
120     self.c = nil;
121     
122     [super dealloc];
123 }
124 
125 @end
126 
127 #import <Foundation/Foundation.h>
128 #import "Computer.h"
129 #import "Person.h"
130 
131 int main(int argc, const char * argv[]) {
132     @autoreleasepool {
133         Computer *c = [[Computer alloc] initWithWidth:20.5 andName:@"Apple"];
134         [c print];
135         
136         Person *p = [[Person alloc] initWithAge:20 andName:@"xiaoming" andComputer:c];
137         [p print];
138         
139         Person *p1 = [p copy];
140         [p1 print];
141         
142         NSString *path = @"/Users/hskj/Documents/hello.txt";
143         [NSKeyedArchiver archiveRootObject:p toFile:path];
144         Person *p2 = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
145         [p2 print];
146         
147         [p1 release];
148         [p release];
149         [c release];
150     }
151     return 0;
152 }