Lan Yi IOS Nscoder

Source: Internet
Author: User

Person.h Header File Contents

    1. #import <Foundation/Foundation.h>
    2. @interface Person:nsobject
    3. {
    4. NSString *name;
    5. char sex;
    6. int age;
    7. }
    8. @property (nonatomic,copy) NSString *name;
    9. @property char sex;
    10. @property int age;
    11. @end

PERSON.M implementation file

    1. #import "Person.h"

    1. @implementation person
    2. @synthesize Name,age,gender;
    3. -(void) Encodewithcoder: (Nscoder *) Acoder
    4. {
    5. [Acoder encodeobject:name forkey:@ "Stuname"];
    6. NSNumber *age1 = [NSNumber numberwithint:age];
    7. [Acoder encodeobject:age1 forkey:@ "Stuage"];
    8. NSNumber *gender1 = [NSNumber Numberwithchar:gender];
    9. [Acoder encodeobject:gender1 forkey:@ "Stugender"];
    10. }
    11. -(ID) Initwithcoder: (Nscoder *) Dcoder
    12. {
    13. name = [Dcoder decodeobjectforkey:@ "Stuname"];
    14. age = [[Dcoder decodeobjectforkey:@ "Stuage"]intvalue];
    15. gender = [[Dcoder decodeobjectforkey:@ "Stugender"]charvalue];
    16. return self;
    17. }
    18. @end

MAIN.M test File

  1. #import <Foundation/Foundation.h>
  2. #import "Person.h"
  3. int main (int argc, const char * argv[]) {
  4. @autoreleasepool {
  5. Person *P1 = [[Person alloc]init];
  6. P1.name = @ "Zhangsan";
  7. P1.age = 22;
  8. P1.gender = ' F ';
  9. Person *P2 = [person new];
  10. P2.name = @ "Lisi";
  11. P2.age = 23;
  12. P2.gender = ' M ';
  13. Nsmutabledictionary *dic = [Nsmutabledictionary dictionarywithcapacity:30];
  14. [DiC setobject:p1 forkey:@ "Person1"];
  15. [DiC setobject:p2 forkey:@ "Person2"];
  16. To deposit the contents of a dictionary into a file
  17. if ([Nskeyedarchiver archiverootobject:dic tofile:@ "/users/mac/desktop/a.plist"]) {
  18. NSLog (@ "Chenggong");
  19. }
  20. Nsmutabledictionary *dic2 = [Nsmutabledictionary dictionarywithcapacity:30];
  21. Reading content from a file into a dictionary
  22. Dic2 = [Nskeyedunarchiver unarchiveobjectwithfile:@ "/users/mac/desktop/a.plist"];
  23. Nsenumerator *enumerator = [Dic2 objectenumerator];
  24. ID obj;
  25. Traversing dictionary Contents
  26. while (obj = [Enumerator nextobject]) {
  27. NSLog (@ "name =%@,age =%i,gender =%c", [obj name],[obj age],[obj gender]);
  28. }
  29. }
  30. return 0;
  31. }

Lan Yi IOS Nscoder

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.