iOS Stage Learning 17th Day notes (operation of files in OC)

Source: Internet
Author: User

iOS Learning (OC language) Knowledge Point finishing

One, single case mode

1) A single case is a programming idea, a design pattern, and language-independent in an application that uses a singleton object, a singleton class is required to provide an instance of the Singleton object in its own case.

No matter how many times the Singleton object is instantiated, only one object exists, the object is global, can be shared by the entire application, access

2) Use the class method provided by the class to get the singleton object using the singleton mode, and try not to get the Singleton object with Alloc init method

3) A singleton class provides class methods for obtaining singleton objects when class methods generally start with SHAREDXX/STANDEDXX/DEFAULTXX

Instance code:

#import"Plane.h"StaticPlane *plane=nil;//to define a static global variable@implementation Plane//ways to get Singleton objects+ (Plane *) sharedpalne{//Resolve data Synchronization (when one thread accesses, other threads cannot manipulate@synchronized (self) {if(plane==Nil) {Plane=[[Plane alloc]init]; }    }    returnplane;}//This method is executed when the Alloc method is called+ (ID) Allocwithzone: (struct_nszone *) zone{NSLog (@"%@", Nsstringfromselector (_cmd)); if(plane==Nil) {Plane=[Super Allocwithzone:zone]; }    returnplane;} @end

Ii. operation of Nsfilemanager files/folders

1) Nsfilemanager a singleton class for managing files or directories (folders).

2) Instantiation methods for file management classes such as:

1  Nsfilemanager *fm=[nsfilemanager Defaultmanager];

3) Fileexistsatpath is used to determine if a file exists such as:

1 BOOL ret=[fm fileexistsatpath:@ "/users/kingkong/desktop/test/test.txt"];

4) Createfileatpath used to create a file the first parameter is the file name, the second parameter is the initial value of the files, and the third parameter is the file's properties, for example:

1 BOOL ret=[fm createfileatpath:@ "/users/kingkong/desktop/test/test.txt" Contents:nil Attributes:nil];

5) Copyitematpath used to copy files: Copy one file to another file, third parameter: Callback error message for example:

1Nserror *error=nil;//used to receive error messages2 3NSString *curpath=@"/users/kingkong/desktop/test/test.txt ";4 5 nsstring *[email protected]"/users/kingkong/desktop/test/test2.txt ";6 7RET=[FM Copyitematpath:curpath Topath:topath error:&ERROR];8 9         if(error!=Nil) {Ten  OneNSLog (@"error:%@", [Error userInfo]); A  -}

6) Moveitematpath for moving/renaming files, moving files under one path to a file under another path (the destination path must specify the file name),

If the source path differs from the file name under the destination path, rename for example:

1 nsstring  *curpath=@ "/users/kingkong/desktop/test/test.txt"; 2 3 nsstring  *[email protected]"/users/kingkong/desktop/test/test2.txt"; 4 5 [FM moveitematpath:curpath Topath:topath Error:nil];

7) Attributesofitematpath Gets the properties of the file such as:

1 nsstring  *curpath=@ "/users/kingkong/desktop/test/test.txt"; 2 3 nsdictionary *dict= [FM attributesofitematpath:curpath Error:nil]; 4 5 // gets the size of the file in the property dictionary 6 7 NSLog (@ "size:%@", [Dict objectforkey:nsfilesize]);

8) removeItemAtPath is used to delete files under the specified path, for example:

1 nsstring  *path=@ "/users/kingkong/desktop/test/test.txt"; 2 3 BOOL ret=[fm Removeitematpath:path Error:nil];

9) NSData: When working with files (writing data to files and reading data from a file) and fetching data from the network, you need to convert the data into a purely binary form,

NSData is the binary form of the cache object, which is equivalent to Char buf[255 in C] for example:

1 nsstring  *path=@ "/users/kingkong/desktop/test/test.txt"; 2 3

NSData data into NSString type data such as:

1  // nsdata---->nsstring to convert NSData objects to strings 2 3 nsstring *scontent=[[nsstring alloc]initwithdata:content  encoding:nsutf8stringencoding];      

One) the nsstring type of data is converted to NSData data such as:

1 nsstring *str=@ "Hello china! " ; 2 3 // nsstring---->NSData; convert strings to NSData data (write to file or transfer to network) 4 5 nsdata *data2=[str datausingencoding:nsutf8stringencoding];

Createfileatpath ... contents build the file and specify what to write for example:

1 nsstring  *path=@ "/users/kingkong/desktop/test/test.txt"; 2 3 [FM Createfileatpath:path contents:data2  Attributes:nil];

Fileexistsatpath ... isdirectory determine if a directory exists (in the case of a file/directory, the second parameter gets whether it is a directory/file) For example:

1 BOOL isdir; // possible results are: -1,0,1 2 3 nsstring  *path=@ "/users/kingkong/desktop/test/test.txt"; 4 5 BOOL ret=[fm Fileexistsatpath:path isdirectory:&isdir];

Contentsofdirectoryatpath shallow traversal: Gets subdirectories and filenames under directory such as:

1 nsstring  *path=@ "/users/kingkong/desktop/test"; 2 3 nsarray *array=[fm Contentsofdirectoryatpath:path Error:nil]; 4 5 NSLog (@"array%@", array);

Subpathsatpath deep traversal: Gets all the contents of the directory (including all content under subdirectories), for example:

1 nsstring  *path=@ "/users/kingkong/desktop/test"; 2 3 Nsarray *array2=[fm Subpathsatpath:path]; 4 5 NSLog (@"array2:%@", array2);

Currentdirectorypath get the directory where the current application is located for example:

1 nsstring* currentpath=[FM Currentdirectorypath]; 2 3 NSLog (@ "%@", Currentpath);

Createdirectoryatpath ... withintermediatedirectories create a directory, the first parameter is the directory name, the second parameter is

Whether you need to create a directory that does not exist at the top level (intermediate directory) when the directory does not exist

1 nsstring  *path=@ "/users/kingkong/desktop/test/file"; 2 3 BOOL ret=[fm createdirectoryatpath:path withintermediatedirectories:yes attributes:nil Error:nil];

 

Third, nsfilehandle the file read and write operations

1) Filehandleforreadingatpath Create a read-only file object (handle) For example:

1 nsstring *path=@ "/users/kingkong/desktop/test/test.txt"; 2 3 nsfilehandle *readhandle=[nsfilehandle Filehandleforreadingatpath:path];

2) Readdataoflength read the contents of the specified length in the file such as:

NSData *data=[readhandle readdataoflength:3*sdata=[[NSString Alloc]initwithdata:data Encoding:nsutf8stringencoding]; NSLog (@ "%@", sdata);

3) Readdatatoendoffile reads the contents from the current position to the end of the file for example:

1 data=[Readhandle readdatatoendoffile]; 2 3 sdata=[[NSString alloc]initwithdata:data encoding:nsutf8stringencoding]; 4 5 NSLog (@ "%@", sdata); 6 7 [Readhandle CloseFile]; // read the end of the file (note that if the last read did not close the read again, it will begin reading from the last read end position)

4) Filehandleforwritingatpath Create an object that writes only files, for example:

1Nsfilehandle *writehandle=[nsfilehandle Filehandleforwritingatpath:path"];2 3NSString *s1=@"Hello";4 5 //Seektoendoffile moving the object pointer to the end of the file (implementing append)6 7 [Writehandle seektoendoffile];8 9 [Writehandle writedata:[s1 datausingencoding:nsutf8stringencoding];Ten  One  //synchronizefile writing data to a file synchronously A  - [Writehandle synchronizefile]; -  the[Writehandle CloseFile];//indicates end of Operation

5) Truncatefileatoffset truncates the contents of the file starting from the specified position character, preserves the number of characters, sets to 0, and writes the equivalent of emptying overrides such as:

1  [Writehandle truncatefileatoffset:4];

6) Seektofileoffset writes the file from the specified position character, overwriting some of the contents such as:

1    [Writehandle seektofileoffset:2];

Iv. Operations on path (file path)

1) pathcomponents get the parts of the path for example:

1 nsstring *path=@ "/users/kingkong/desktop/test/text.txt"; 2 3 nsarray *array1= [path pathcomponents]; 4 5 NSLog (@"array1:%@", array1);

2) pathextension get the file suffix name for example:

1 NSLog (@ "%@", [path pathextension]); // results: TXT

3) Isabsolutepath Determine if it is an absolute path: The path with/begins for example:

1  NSLog (@ "%d", [path Isabsolutepath]); // Results: 1

4) Lastpathcomponent get the last part of the path for example:

1  NSLog (@ "last:%@", path.lastpathcomponent); // results: Text.txt

5) stringbyappendingpathcomponent Add another path after one path for example:

1 NSLog (@ "%@", [path stringbyappendingpathcomponent:@ "files"]); 2 3 //Result:/users/kingkong/desktop/test/text.txt/files

6) stringbyappendingpathextension Add extension for path for example:

1 NSLog (@ "%@", [path stringbyappendingpathextension:@ "m  "]); 2 3 // results:/USERS/KINGKONG/DESKTOP/TEST/TEXT.TXT.M

iOS Stage Learning 17th Day notes (operation of files in OC)

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.