Add the Address Book class (name, phone), archive and archive method, that is, add the method of storing information to disk.

Source: Internet
Author: User

/*1. Add the Address Book class (name, phone) to the archive and archive method, that is, add the method that stores the information to the disk. */

#import <Foundation/Foundation.h>
#import "ConTact.h"
#define PATH @ "/users/qianfeng/desktop/hehe/2.plist"

int main (int argc, const char * argv[]) {
@autoreleasepool {
Contact *contact = [[Contact alloc] init];
Contact.name = @ "Xiaowang";
Contact.tel = @ "135";

Creating a mutable binary file
Nsmutabledata *mdata = [[Nsmutabledata alloc] init];
Create an Archive object
Nskeyedarchiver *keyarchiver = [[Nskeyedarchiver alloc]initforwritingwithmutabledata:mdata];
Archived objects
[Keyarchiver encodeobject:contact forkey:@ "name"];
Complete archiving
[Keyarchiver finishencoding];
Write file
[Mdata Writetofile:path Atomically:yes];
printf ("...... ..... ..... ..... ..... .....)..............
To read a binary file
NSData *data = [[NSData alloc] initwithcontentsoffile:path];
Create a solution Archive object
Nskeyedunarchiver *keyunarchiver = [[Nskeyedunarchiver alloc] initforreadingwithdata:data];
Solution Archive
Contact *c = [keyunarchiver decodeobjectforkey:@ "name"];

Release Archive
[Keyunarchiver finishdecoding];
NSLog (@ "%@,%@", C.name,c.tel);


}
return 0;
}

Add the Address Book class (name, phone), archive and archive method, that is, add the method of storing information to disk.

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.