If the class follows the Nscoding protocol, the two instance methods of encoding and decoding the Protocol must be implemented in the class. This feature provides the underlying archive and file functionality.
Small exercise:
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. 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 the copy function to create the person class object P1 and output the object information.
- C. put pp into file hello.txt, file directory is documents directory.
- D. Read object P2 from file Hello.txt and output object information
- E. Memory leaks and multiple deletions cannot occur.
The implementation is as follows:
Objective-c the Nscoding protocol for file operation