IOS learning 17th days of notes (operations on files in OC), ios 17th days

Source: Internet
Author: User

IOS learning 17th days of notes (operations on files in OC), ios 17th days

Knowledge points of IOS Learning (oc language)

I. Singleton Mode

 

1) Singleton is a programming idea. A design model is language-independent. In applications that use Singleton objects, the singleton class needs to provide the instantiated singleton object by itself,

No matter how many times a singleton object is instantiated, only one object exists. This object is global and can be shared and accessed by the entire application.

 

2) in singleton mode, use the class method provided by the class to obtain the singleton object. Try not to use the alloc init method to obtain the singleton object.

 

3) when the singleton class provides a class method to obtain the singleton object, the class method generally starts with sharedXX/standedXX/defaultXX.

Instance code:

# Import "Plane. h "static Plane * plane = nil; // defines a static global variable @ implementation Plane // method for obtaining the singleton object + (Plane *) sharedPalne {// solve data synchronization (when a thread is accessed, other threads cannot operate @ synchronized (self) {if (plane = nil) {plane = [[Plane alloc] init] ;}} return plane ;}// 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];} return plane ;} @ end

 

2. NSFileManager file/folder operations

 

1) NSFileManager is a singleton class used to manage files or directories (folders.

 

2) The file management class instantiation method is as follows:

1  NSFileManager *fm=[NSFileManager defaultManager];

3) fileExistsAtPath is used to determine whether a file exists. For example:

1 BOOL ret=[fm fileExistsAtPath:@"/Users/kingkong/Desktop/Test/test.txt"];

 

4) createFileAtPath is used to create a file. The first parameter is the file name, the second parameter is the initial value of the file, and the third parameter is the file attributes, for example:

1 BOOL ret=[fm createFileAtPath:@"/Users/kingkong/Desktop/Test/test.txt" contents:nil attributes:nil];

5) copyItemAtPath is used to copy a file: copy a file to another file. The third parameter: callback error message, for example:

1 NSError * error = nil; // used to receive error messages 2 3 NSString * curpath = @ "/Users/kingkong/Desktop/Test/test.txt "; 4 5 NSString * topath = @ "/Users/kingkong/Desktop/Test/test2.txt"; 6 7 ret = [fm copyItemAtPath: curpath toPath: topath error: & error]; 8 9 if (error! = Nil) {10 11 NSLog (@ "error: % @", [error userInfo]); 12 13}

6) moveItemAtPath is used to move or rename a file. It moves the file in a path to another path (the file name must be specified in the target path ),

If the file name in the Source Path is different from that in the target path, rename the file, for example:

1 NSString  *curpath=@"/Users/kingkong/Desktop/Test/test.txt”;2 3 NSString  *topath=@"/Users/kingkong/Desktop/Test/test2.txt”;4 5 [fm moveItemAtPath: curpath toPath: topath error:nil];

 

7) attributesOfItemAtPath:

1 NSString * curpath = @ "/Users/kingkong/Desktop/Test/test.txt"; 2 3 NSDictionary * dict = [fm attributesOfItemAtPath: curpath error: nil]; 4 5 // obtain the file size 6 7 NSLog (@ "size: % @", [dict objectForKey: NSFileSize]) in the attribute dictionary;

 

8) removeItemAtPath is used to delete objects in a 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 you operate on a file (write data to a file and read data from the file) and obtain data from the network, you need to convert the data to a pure binary form,

NSData is a binary cached object, which is equivalent to char buf [255] in C. For example:

1 NSString  *path=@"/Users/kingkong/Desktop/Test/test.txt”;2 3 NSData *content=[fm contentsAtPath: path]; 

10) NSData to NSString type data, for example:

1 // NSData ----> NSString, convert NSData object to string 2 3 NSString * scontent = [[NSString alloc] initWithData: content encoding: NSUTF8StringEncoding];

11) NSString-type data is converted to NSData, for example:

1 NSString * str = @ "hello China! "; 2 3 // NSString ----> NSData; convert the string to NSData (writing a file or transmitting it to the network) 4 5 NSData * data2 = [str dataUsingEncoding: NSUTF8StringEncoding];

 

12) createFileAtPath... Contents creates a file and specifies the written content, for example:

1 NSString  *path=@"/Users/kingkong/Desktop/Test/test.txt”;2 3 [fm createFileAtPath:path contents:data2  attributes:nil];

 

13) fileExistsAtPath... IsDirectory determines whether a directory exists (if a file or directory exists, use the second parameter to obtain whether the directory or file exists). For example:

1 BOOL isDir; // possible results include-, 12 3 NSString * path = @ "/Users/kingkong/Desktop/Test/test.txt "; 4 5 BOOL ret = [fm fileExistsAtPath: path isDirectory: & isDir];

 

14) contentsOfDirectoryAtPath: Obtain the subdirectories and file names in the directory, for example:

1 NSString  *path=@"/Users/kingkong/Desktop/Test”;2 3 NSArray *array=[fm contentsOfDirectoryAtPath: path error:nil];4 5 NSLog(@"array%@",array);

 

15) subpathsAtPath deep traversal: Get all contents in the directory (including all contents in the subdirectory) for example:

1 NSString  *path=@"/Users/kingkong/Desktop/Test”;2 3 NSArray *array2=[fm subpathsAtPath:path];4 5 NSLog(@"array2:%@",array2);

 

16) currentDirectoryPath:

1 NSString* currentPath=[fm currentDirectoryPath];2 3 NSLog(@"%@",currentPath);

 

17) createDirectoryAtPath... WithIntermediateDirectories: create a directory. The first parameter is the directory name, and the second parameter is

If the directory does not exist, do you need to create a directory that does not exist at the upper level (intermediate directory)

1 NSString  *path=@"/Users/kingkong/Desktop/Test/File”;2 3 BOOL ret=[fm createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];

Iii. NSFileHandle

 

1) fileHandleForReadingAtPath: Create an object (handle) for a read-only file. For example:

1 NSString *path=@"/Users/kingkong/Desktop/Test/test.txt”;2 3 NSFileHandle *readHandle=[NSFileHandle fileHandleForReadingAtPath: path];

 

2) readDataOfLength:

NSData *data=[readHandle readDataOfLength:3];NSString *sdata=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];NSLog(@"%@",sdata);

 

3) readDataToEndOfFile reads content from the current location until 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]; // The end of reading the file content (Note that if the last read is not closed, the read will start from the end position of the last read)

 

4) fileHandleForWritingAtPath:

1 NSFileHandle * writeHandle = [NSFileHandle fileHandleForWritingAtPath: path "]; 2 3 NSString * s1 = @" hello "; 4 5 // seekToEndOfFile move the object pointer to the end of the file (Append content) 6 7 [writeHandle seekToEndOfFile]; 8 9 [writeHandle writeData: [s1 dataUsingEncoding: Unknown]; 10 11 // synchronizeFile synchronously writes data to file 12 13 [writeHandle synchronizeFile]; 14 15 [writeHandle closeFile]; // indicates that the operation is complete.

5) truncateFileAtOffset truncates the file content starting from the specified position. The number of characters to be retained is set to 0, and then the write operation is equivalent to clearing and rewriting. For example:

1  [writeHandle truncateFileAtOffset:4];

 

6) seekToFileOffset writes data to the file starting from the specified position, covering part of the content such:

1    [writeHandle seekToFileOffset:2];

 

4. Path operations

 

1) pathComponents:

1 NSString *path=@"/Users/kingkong/Desktop/Test/text.txt”;2 3 NSArray *array1= [path pathComponents];4 5 NSLog(@"array1:%@",array1);

 

2) pathExtension:

1 NSLog (@ "% @", [path pathExtension]); // result: txt

 

3) isAbsolutePath: determines whether the path starts with a slash (/). For example:

1 NSLog (@ "% d", [path isAbsolutePath]); // result: 1

 

4) lastPathComponent:

1 NSLog (@ "last: % @", path. lastPathComponent); // result: 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) Add a Path Extension such as stringByAppendingPathExtension:

1 NSLog (@ "% @", [path stringByAppendingPathExtension: @ "m"]); 2 3 // result:/Users/kingkong/Desktop/Test/text.txt. m

 

Related Article

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.