//
Viewcontroller.m
Sandbox operations
//
Created by Mncong on 15/11/26.
COPYRIGHT©2015 year Mancong. All rights reserved.
//
#import "ViewController.h"
@interface Viewcontroller ()
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
Reach the Sandbox root path
[Self gethomepath];
Get Document Path
[Self getdocumentspath];
Get the Library directory path
[Self getlibrarypath];
Get the cache path in the library
[Self getcachepath];
Get temp Path
[Self GetTempPath];
Processing of NSString class paths
[Self dealwithpath];
NSData Processing
[Self dealwithdata];
File Management
[Self dealwithnsafilemanager];
Get a list of files and files in a 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
{
Path of the action
NSString * Path = @ "/users/apple/testfile.text";
Get the various components of this path
Nsarray * arr = [path pathcomponents];
NSLog (@ "%@", arr);
Extract the last component of this path
NSString * lastcomponent = [path lastpathcomponent];
NSLog (@ "%@", lastcomponent);
Delete the last component of a path
NSString * deletelastcomponent = [path stringbydeletinglastpathcomponent];
NSLog (@ "%@", deletelastcomponent);
Add path to the end of the first mail path
NSString * Addpathtopath = [path Stringbyappendingpathcomponent:path];
NSLog (@ "%@", Addpathtopath);
Remove the extension from the last part of the path
NSString * Extension = [path pathextension];
NSLog (@ "%@", extension);
Remove the extension from the last part of the path
NSString * deletepathextension = [path stringbydeletingpathextension];
NSLog (@ "%@", deletepathextension);
Append extension to the last part of the path (note: The method has been spliced with a dot number, do not add)
NSString * appendextension = [path stringbyappendingpathextension:@ "jpg"];
NSLog (@ "%@", appendextension);
}
-(void) Dealwithdata
{
1.NSString and NSData Conversion
NSString * string1 = @ "1234ABCD";
NSData * data1 = [string1 datausingencoding:nsutf8stringencoding];
NSLog (@ "data1:%@", data1);
NSString * string2 = [[NSString alloc] initwithdata:data1 encoding:nsutf8stringencoding];
NSLog (@ "string2:%@", string2);
2.NSData and UIImage
Stitching in the path under this project
NSString * Path = [[NSBundle mainbundle] bundlepath];
NSString * name = [NSString stringwithformat:@ "1.jpg"];
NSString * Finalpath = [path stringbyappendingpathcomponent:name];
Change path to NSData type
NSData * ImageData = [NSData Datawithcontentsoffile:finalpath];
Get Pictures
UIImage * image = [UIImage imagewithdata:imagedata];
displayed on the 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 document Directory
NSString * Documentsdirectory = [Nshomedirectory () stringbyappendingpathcomponent:@ "Documents"];
NSLog (@ "documentsdirectory:%@", documentsdirectory);
Create a Directory
What does it mean after #warning format and the arguments behind it
[[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 file
[Str writetofile:filepath atomically:yes encoding:nsutf8stringencoding error:&error];
Displaying the contents of a file directory
NSLog (@ "documentsdirectory:%@", [FileManager contentsofdirectoryatpath:documentsdirectory error:&error]);
2. Renaming 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 a file
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);
Lists all subfolder names 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
iOS Learning-Sandbox path summary, file Management Nsfilemanager Summary