IOS learning-sandbox path summary, file management NSFileManager summary, Sandbox nsfilemanager
//
// ViewController. m
// Sandbox operation
//
// Created by mncong on 15/11/26.
// Copyright©2015 mancong. All rights reserved.
//
# Import "ViewController. h"
@ Interface ViewController ()
@ End
@ Implementation ViewController
-(Void) viewDidLoad {
[Super viewDidLoad];
// Reach the sandbox root path
[Self getHomePath];
// Obtain the Document path
[Self getDocumentsPath];
// Obtain the Library directory path
[Self getLibraryPath];
// Obtain the Cache path in the Library
[Self getCachePath];
// Obtain the temp path
[Self getTempPath];
// NSString class path Processing
[Self dealWithPath];
// NSData Processing
[Self dealWithData];
// File Management
[Self dealWithNSaFileManager];
// Obtain the list of files and files in the file
[Self getDirectorys];
}
-(Void) getHomePath
{
NSString * homePath = NSHomeDirectory ();
NSLog (@ "app_home: % @", homePath );
}
-(NSString *) getDocumentsPath
{
NSArray * pathsArr = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
NSLog (@ "% @", pathsArr );
NSString * documentsPath = [pathsArr lastObject];
NSLog (@ "app_documentsPath: % @", documentsPath );
Return documentsPath;
}
-(Void) getLibraryPath
{
NSArray * pathsArr = NSSearchPathForDirectoriesInDomains (NSLibraryDirectory, NSUserDomainMask, YES );
NSString * documentsPath = [pathsArr lastObject];
NSLog (@ "app_documentsPath: % @", documentsPath );
}
-(Void) getCachePath
{
NSArray * pathsArr = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES );
NSString * cachePath = [pathsArr lastObject];
NSLog (@ "cachePath: % @", cachePath );
}
-(Void) getTempPath
{
NSString * tempPath = NSTemporaryDirectory ();
NSLog (@ "app_tempPath: % @", tempPath );
}
-(Void) dealWithPath
{
// Operation path
NSString * path = @ "/Users/apple/testfile. text ";
// Obtain the components of this path
NSArray * arr = [path pathComponents];
NSLog (@ "% @", arr );
// Extract the last part of this path
NSString * lastComponent = [path lastPathComponent];
NSLog (@ "% @", lastComponent );
// Delete the last part of the path
NSString * deleteLastComponent = [path stringByDeletingLastPathComponent];
NSLog (@ "% @", deleteLastComponent );
// Add the path to the end of the first mail path
NSString * addPathToPath = [path stringByAppendingPathComponent: path];
NSLog (@ "% @", addPathToPath );
// Remove the extension of the last part of the path
NSString * extension = [path pathExtension];
NSLog (@ "% @", extension );
// Delete the extension of the last part of the path
NSString * deletePathExtension = [path stringByDeletingPathExtension];
NSLog (@ "% @", deletePathExtension );
// Append the extension to the last part of the path. (Note: The method has already been spliced with a dot. Do not add the dot)
NSString * appendExtension = [path stringByAppendingPathExtension: @ "jpg"];
NSLog (@ "% @", appendExtension );
}
-(Void) dealWithData
{
// 1. NSString and NSData Conversion
NSString * string1 = @ "1234 abcd ";
NSData * data1 = [string1 dataUsingEncoding: NSUTF8StringEncoding];
NSLog (@ "data1: % @", data1 );
NSString * string2 = [[NSString alloc] initWithData: data1 encoding: NSUTF8StringEncoding];
NSLog (@ "string2: % @", string2 );
// 2. NSData and UIImage
// Link the path under the project
NSString * path = [[NSBundle mainBundle] bundlePath];
NSString * name = [NSString stringWithFormat: @ "1.jpg"];
NSString * finalPath = [path stringByAppendingPathComponent: name];
// Change the path to the NSData type
NSData * imageData = [NSData dataWithContentsOfFile: finalPath];
// Obtain the image
UIImage * image = [UIImage imageWithData: imageData];
// Display on UIimageView
UIImageView * imageView = [[UIImageView alloc] initWithFrame: CGRectMake (0, 0, [UIScreen mainScreen]. bounds. size. width, 200)];
ImageView. image = image;
[Self. view addSubview: imageView];
}
-(Void) dealWithNSaFileManager
{
NSError * error;
// Create File Management
NSFileManager * fileManager = [NSFileManager defaultManager];
// Point to the document directory
NSString * documentsDirectory = [NSHomeDirectory () stringByAppendingPathComponent: @ "Documents"];
NSLog (@ "documentsDirectory: % @", documentsDirectory );
// Create a directory
# What is the meaning after warning format and the following parameters?
[[NSFileManager defaultManager] createDirectoryAtPath: [NSString stringWithFormat: @ "% @/myFolder", NSHomeDirectory ()] withIntermediateDirectories: YES attributes: nil error: & error];
// 1. Create a file
NSString * filePath = [documentsDirectory stringByAppendingPathComponent: @ "file1.txt"];
NSLog (@ "filePath: % @", filePath );
NSString * str = @ "string to be written ";
// Write a file
[Str writeToFile: filePath atomically: YES encoding: NSUTF8StringEncoding error: & error];
// Display the contents of the file directory
NSLog (@ "documentsDirectory: % @", [fileManager contentsOfDirectoryAtPath: documentsDirectory error: & error]);
// 2. rename a file
NSString * filePath2 = [documentsDirectory stringByAppendingPathComponent: @ "file2.text"];
NSLog (@ "% @", filePath2 );
If ([fileManager moveItemAtPath: filePath toPath: filePath2 error: & error]! = YES)
NSLog (@ "Unable to mnove file: % @", error. localizedDescription );
NSLog (@ "DocumentsDirectory: % @", [fileManager contentsOfDirectoryAtPath: documentsDirectory error: & error]);
// 3. delete an object
If ([fileManager removeItemAtPath: filePath2 error: & error]! = YES)
NSLog (@ "Unable to delete file: % @", error. localizedDescription );
NSLog (@ "Documentsdirectory: % @", [fileManager contentsOfDirectoryAtPath: documentsDirectory error: & error]);
}
-(Void) getDirectorys
{
NSFileManager * fileManager = [NSFileManager defaultManager];
NSArray * documentsPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
NSString * documentsDirectory = [documentsPaths objectAtIndex: 0];
NSError * error = nil;
NSArray * fileList = [NSArray array];
FileList = [fileManager contentsOfDirectoryAtPath: documentsDirectory error: & error];
NSLog (@ "fileList: % @", fileList );
// List all subfolders in a given folder
NSMutableArray * dirArray = [NSMutableArray array];
BOOL isDir = NO;
For (NSString * file in fileList ){
NSString * path = [documentsDirectory stringByAppendingPathComponent: file];
[FileManager fileExistsAtPath: path isDirectory :( & isDir)];
If (isDir ){
[DirArray addObject: file];
}
IsDir = NO;
}
NSLog (@ "Every Thing in the Dir: % @", fileList );
NSLog (@ "All folders: % @", dirArray );
}
@ End