ios-senior1-Data Processing (file read/write)

Source: Internet
Author: User

Understanding of the 1.Model data layer

1.1 Sandbox Basics

Sandbox definition: Each iOS application creates a file system directory (folder) for itself, a separate, closed, secure space called a sandbox.

Sandbox features:

Sandbox folders and the role of individual folders

Two ways to find a sandbox:

1. Click Finder, go to (upper-left corner)-> hold the ALT key, Developer, Coresimulator, and so on (destination folder)

2. Search-and-end command line:

Display: Defaults write Com.apple.finder appleshowallfiles-bool true
Hidden: Defaults write Com.apple.finder Appleshowallfiles-bool false

1.2 Data localization Knowledge points

Data localization definitions and how:

2. File read/write (storage)

2.1 Simple data types are written locally (strings, arrays, dictionaries, nsdata types of data stored locally) Nsstring,nsarray (for example, string)

Operation Result :

2.2 (store NSData type of data) Read image write Local

The code is as follows:

Operation Result :

2.3 Complex objects stored locally (person)

The source code is as follows:

In the Person.h file:

Name

@property (nonatomic,copy) NSString *name;

Gender

@property (nonatomic,copy) NSString *gender;

Age

@property (nonatomic,assign) Nsinteger age;

In the person.m file:

Archive: Archive all attributes

-(void)encodewithcoder:(Nscoder *) Acoder {

[Acoder encodeObject:self.name forkey:@ "name"];

[Acoder encodeObject:self.gender forkey:@ "gender"];

[Acoder encodeInteger:self.age forkey:@ "age"];

}

Solution (Reverse archive)

-(instancetype)initwithcoder:(Nscoder *) Adecoder {

self = [super init];

if (self) {

Self.name = [Adecoder decodeobjectforkey: @ "name"];

Self.gender = [Adecoder decodeobjectforkey: @ "gender"];

Self.age = [Adecoder decodeintegerforkey: @ "age"];

}

return self;

}

in the viewcontroller.m file :

How to store objects of a person type locally, that is, for the localization of complex objects, this object must adhere to the Nscoding protocol and implement two methods in the Protocol

#pragma mark-Localization of complex objects

#pragma mark-Archive process

1. Find the directory of Documents folder

NSString *documentpath = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, NSUserDomainMask, YES) OBJECTATINDEX:0];

2. Create a Person object

Person *person = [[Person alloc]init];

Person.name = @ "Mbboy";

Person.gender = @ "Boy";

Person.age = 18;

3. Archive this object

3.1 Create Nsmutabledata, used to initialize the archive tool

Nsmutabledata *data = [Nsmutabledata data];

NSLog (@ "data =%@", data);

3.2 Creating an archive tool

Nskeyedarchiver *archiver = [[Nskeyedarchiver alloc]initforwritingwithmutabledata:data];

3.3 Archive The Person object to be archived

[Archiver encodeobject:person forkey:@ "person"];

3.4 End Archive

[Archiver finishencoding];

NSLog (@ "data1 =%@", data);

4. Store archived content on-premises

NSString *personpath = [documentpath stringbyappendingpathcomponent:@ "Person.plist"];

Write path

[Data Writetofile:personpath Atomically:yes];

NSLog (@ "Personpath =%@", Personpath);

#pragma mark-The process of unlocking the document

1. Find the data that will be solved

NSData *resultdata = [NSData Datawithcontentsoffile:personpath];

NSLog (@ "resultdata =%@", resultdata);

2. Create a profile tool

Nskeyedunarchiver *unarchiver = [[Nskeyedunarchiver alloc]initforreadingwithdata:resultdata];

3. Document the Person object [receive with an object, return value]

Person *newperson = [unarchiver decodeobjectforkey:@ ' person '];

4. End the solution

[Unarchiver finishdecoding];

NSLog (@ "name =%@,gender =%@,age =%ld", newperson.name,newperson.gender,newperson.age);

Operation Result :

"Let's do an exercise, let's do it."

Want to know the concrete steps??? Message!!!

ios-senior1-Data Processing (file read/write)

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.