Qustion: (MRC)
1. Define a Computer class
Instance variable: float width; NSString *name;
Method: An initialization function with two parameters; print () function; Dealloc () function;
2. Define a person class
Instance variable: NSString *name; Computer *c; int age;
Method: An initialization function with three parameters; print () function; Dealloc () function;
Requirement: Objects created with this class can copy and save files.
3, the main function requirements:
A) Create the object cc and the Person class object PP for the computer class, respectively, and output the object information.
b) Use copy function to create the person class object P1; and output Object information
c) the PP into the file hello.txt, the file directory is the documents directory.
d) Read the object P2 from the file Hello.txt and output the object information.
e) memory leaks and multiple deletions cannot occur.
---------------------------------------------------------------------------------------------------------
Source Code:
1 #import<Foundation/Foundation.h>2 3 @interfaceComputer:nsobject <NSCoding>4 5@property (nonatomic, assign)floatwidth;6@property (nonatomic, retain) NSString *name;7 8- (ID) Initwithwidth: (float) W Andname: (NSString *) n;9 Ten- (void) print; One A @end - - #import "Computer.h" the - @implementationComputer - -- (ID) Initwithwidth: (float) W Andname: (NSString *) n + { - if(self =[Super init]) + { A_width =W; atSelf.name =N; - } - returnSelf ; - } - -- (void) Print in { -NSLog (@"Width =%f, Name =%@", _width, _name); to } + //Coding -- (void) Encodewithcoder: (Nscoder *) Acoder the { *[Acoder encodeobject:[nsnumber Numberwithfloat:_width] Forkey:@"1"]; $[Acoder encodeobject:_name Forkey:@"2"];Panax Notoginseng } - //decoding the- (ID) Initwithcoder: (Nscoder *) Adecoder + { A if(self =[Super init]) the { +_width = [[Adecoder decodeobjectforkey:@"1"] Floatvalue]; -Self.name = [Adecoder decodeobjectforkey:@"2"]; $ } $ returnSelf ; - } - the- (void) Dealloc - {WuyiSelf.name =Nil; the [Super Dealloc]; - } Wu - @end About $ #import<Foundation/Foundation.h> - - @classcomputer; - @interfacePerson:nsobject <nscoding, nscopying> A +@property (nonatomic, assign)intAge ; the@property (nonatomic, retain) NSString *name; -@property (nonatomic, retain) computer *C; $ the- (ID) Initwithage: (int) a andname: (NSString *) n andcomputer: (Computer *) cc; the the- (void) print; the - @end in the #import "Person.h" the #import "Computer.h" About the @implementation Person the the- (ID) Copywithzone: (Nszone *) Zone + { -Person *p =[[Person Allocwithzone:zone] initwithage:_age andname:_name Andcomputer:_c]; the returnp;Bayi } the the- (void) Encodewithcoder: (Nscoder *) Acoder - { -[Acoder encodeobject:_name Forkey:@"1"]; the[Acoder encodeobject:_c Forkey:@"2"]; the[Acoder encodeobject:[nsnumber Numberwithint:_age] Forkey:@"3"]; the } the -- (ID) Initwithcoder: (Nscoder *) Adecoder the { the if(self =[Super init]) the {94Self.name = [Adecoder decodeobjectforkey:@"1"]; theSELF.C = [Adecoder decodeobjectforkey:@"2"]; the_age = [[Adecoder decodeobjectforkey:@"3"] intvalue]; the }98 returnSelf ; About } - 101- (ID) Initwithage: (int) a andname: (NSString *) n andcomputer: (Computer *) cc102 {103 if(self =[Super init])104 { the_age =A;106Self.name =N;107SELF.C =cc;108 }109 returnSelf ; the }111 the- (void) Print113 { theNSLog (@"Age =%d, Name =%@, computer =%@", _age, _name, _c); the } the 117- (void) Dealloc118 {119Self.name =Nil; -SELF.C =Nil;121 122 [Super Dealloc];123 }124 the @end126 127 #import<Foundation/Foundation.h> - #import "Computer.h"129 #import "Person.h" the 131 intMainintargcConst Char*argv[]) { the @autoreleasepool {133Computer *C = [[Computer alloc] Initwithwidth:20.5Andname:@"Apple"];134 [C print];135 136Person *p = [[Person alloc] Initwithage: -Andname:@"xiaoming"Andcomputer:c];137 [P print];138 139Person *P1 =[P copy]; $ [P1 print];141 142NSString *path =@"/users/hskj/documents/hello.txt";143 [Nskeyedarchiver archiverootobject:p Tofile:path];144Person *P2 =[Nskeyedunarchiver Unarchiveobjectwithfile:path];145 [P2 print];146 147 [P1 release];148 [P release];149 [C release]; Max }151 return 0; the}
Objective--c Practice and source code