Sandbox fast storage and reading
Storage: [[nsuserdefaults Standarduserdefaults] setobject:@ "abc" forkey:@ "1"];
[[Nsuserdefaults standarduserdefaults] synchronize];
READ: NSString *str = [[Nsuserdefaults standarduserdefaults] objectforkey:@ "1"];
Get the location of some directories
1. Get document
NSString *document = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES). LastObject;
NSLog (@ "%@", document);
2. Get the cache
NSString *cache = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES). LastObject;
NSLog (@ "%@", cache);
3. Get preference
NSString *preference = Nssearchpathfordirectoriesindomains (Nspreferencepanesdirectory, NSUserDomainMask, YES). Lastobject;
NSLog (@ "%@", preference);
4. Get TMP
NSString *tmp = Nstemporarydirectory ();
NSLog (@ "%@", TMP);
5. Get the path to the bundle
NSString *bundle = [NSBundle mainbundle].bundlepath;
NSLog (@ "%@", bundle);
Write the file into the sandbox
1. String Write
1.1 Stitching a good path
NSString *stringfilepath = [Document stringbyappendingpathcomponent:@ "String.text"];
NSLog (@ "%@", Stringfilepath);
1.2 Preparing the text to be written
NSString *str = @ "I am the text to be written";
1.3 Writing text to the text price
[STR writetofile:stringfilepath//write to file location
Atomically:yes//Safety
Encoding:nsutf8stringencoding//encoding
Error:nil]; Error
1.4 Reading the contents of a file
NSString *newstr = [NSString stringwithcontentsoffile:stringfilepath encoding:nsutf8stringencoding Error:nil];
NSLog (@ "%@", newstr);
2. Arrays
NSString *arrayfilepath = [Cache stringbyappendingpathcomponent:@ "Array.text"];
Nsarray *arr = @[@ "Gou onr", @ "blot", @ "Dhfjsk"];
[arr Writetofile:arrayfilepath Atomically:yes];
Nsarray *newaarry = [Nsarray Arraywithcontentsoffile:arrayfilepath];
NSLog (@ "%@", Newaarry);
3. Dictionary
NSString *dictfilepath = [Cache stringbyappendingpathcomponent:@ "Dict.text"];
Nsdictionary *dict = @{
@ "name": @ "Gold one",
@ "Age": @ "18",
@ "Gender": @ "female"
};
[Dict Writetofile:dictfilepath Atomically:yes];
Nsdictionary *newdict = [Nsdictionary Dictionarywithcontentsoffile:dictfilepath];
NSLog (@ "%@", newdict);
4. Picture
1. Stitching Address
NSString *imagefilepath = [Document stringbyappendingpathcomponent:@ "Guoguo.png"];
2. Get Data
UIImage *image = [UIImage imagenamed:@ "Guoguo"];
3. Write
3.1 First convert image to NSData data type
NSData *imagedata = uiimagepngrepresentation (image); Convert picture to Data
3.2 Writing to data
[ImageData Writetofile:imagefilepath Atomically:yes];
4. Read
UIImage *newimage = [UIImage Imagewithcontentsoffile:imagefilepath];
NSLog (@ "%@", newimage);
Nsfilemanager
Singleton design Pattern: The object created by the Singleton class will have only one, no matter how many times it is created, it is the same object
Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];
1. Create a folder and stitch the path to the new folder
NSString *filepath = [tmp stringbyappendingstring:@ "Test2/test3/test4"];
2. Create
[FileManager Createdirectoryatpath:filepath//
Withintermediatedirectories:yes//Decide whether you can create a folder that contains relationships
Attributes:nil//
Error:nil]; //
NSLog (@ "%@", FilePath);
3. Delete (this will only delete one of the innermost folders)
[FileManager Removeitematpath:filepath Error:nil];
4. Determine if the file exists
if ([FileManager Fileexistsatpath:stringfilepath]) {
NSLog (@ "file exists");
} else {
NSLog (@ "no");
}
5. Delete all Files
NSString *filepath2 = [tmp stringbyappendingstring:@ "test2"];
[FileManager removeitematpath:filepath2 Error:nil];
6: Get all the files under a folder
Nsarray *filearray = [FileManager contentsofdirectoryatpath:document error:nil];
NSLog (@ "Filearray =%@", Filearray);
iOS sandbox mechanism---access to basic data types and basic operation of files