Objective-c the Nscoding protocol for file operation

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.