iOS data storage

Source: Internet
Author: User

1. File storage

iOS sandbox mechanism: applications can only access directories created by the application in the file system,

Documents folder: Document folder, storing persisted data,

Library folder: Caches: Store cache files, restart or exit the program, the data is not lost

Perferences: Preferences, store user settings information

TMP folder: Store temporary cache data, empty when restarting or exiting the program;

Basic file operations:

-(void) Viewdidload

{

[Super Viewdidload];

File Manager

Nsfilemanager

Create a file Management object

Nsfilemanager *FM = [Nsfilemanager defaultmanager];

Get Documents folder path

Nsarray *array = Nssearchpathfordirectoriesindomains(nsdocumentdirectory, Nsuserdomainmask, YES );

nsstring *documentpath = [array objectatindex:0];

Create a folder

1. create a path to a folder

NSString *directorypath = [documentpath stringbyappendingpathcomponent:@ "testfile"];//set folder path, The stringByAppendingPathComponent method automatically uses "/" to separate the full path,

[FM createdirectoryatpath:d irectorypath withintermediatedirectories:YES attributes:Nil error:nil];

2. Create a file

The path of the stitching file

NSString *filepath = [DirectoryPath stringbyappendingpathcomponent:@ "file"];

NSData used to package data.

Binary data is stored.

Can save various data strings audio image Array dictionary

Convert a string to data

nsstring *string = @ " Hello ";

UTF8 Encoding

NSData *data = [string datausingencoding: nsutf8stringencoding];

Create a file

[FM createfileatpath: FilePath contents:d ata attributes:nil];

Determine if a file exists

BOOL success = [FM fileexistsatpath:filepath];

NSLog (@ " file%d exists ", success);

To get the path of all the files under a path

Nsarray *subpatharray = [FM subpathsatpath:directorypath];

For (nsstring *path in subpatharray)

{

NSLog (@ "---->%@", path);

}

nsstring *path2 = [DirectoryPath stringbyappendingpathcomponent:@ "file copy "];

Determine if content is equal

Contentsequalatpath

BOOL SUCCESS1 = [fm contentsequalatpath:filepath andpath:path2];

NSLog(@ "Success1 = =%d", success1);

//Move an element under a path to another path

NSString *destinationpath = [nshomedirectory() stringbyappendingpathcomponent:@ "A.txt"];

Move a file to a file path (not a directory)

[FM moveitematpath:filepath topath:destinationpath error:nil];

Copy to a file path

[FM copyitematpath:destinationpath topath:filepath error:nil];

Remove a file from a path

[FM removeitematpath:filepath error:nil];

}

iOS data storage

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.