IOS-Plist file storage (1), iosplist File
1. What is a file system?
In IOS, each app has its own file system and corresponding access permissions, which are generally divided
~ /Documents/
~ /Tmp/
~ /Library/Caches/
~ /Library/Preferences/------- key-value pairs. do not care about the file path.
The path is obtained as follows:
<Span style = "color: #999999;"> {// obtain the main directory NSString * path = NSHomeDirectory (); NSString * docPath = [path stringByAppendingPathComponent: @ "Documents"]; NSLog (@ "% @", docPath); // obtain the file directory NSArray * DocumentPath = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSAllDomainsMask, YES); // NSLog (@ "% @", documentPath [0]); // obtain the cache directory NSArray * cachePath = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSAllDomainsMask, YES); // NSLog (@ "% @", cachePath [0]); // obtain the temporary directory NSString * temp = NSTemporaryDirectory (); // NSLog (@ "% @", temp) ;}</span>
Plist files can only store NSString NSNumber NSData NSArray NSDictionary content, and their files are stored in xml format.
NSArray is stored in Documents:
NSArray * arr = @ [@ "name", @ "age", @ "height"]; NSString * path = NSHomeDirectory (); NSString * docPath = [path stringByAppendingPathComponent: @ "Documents"]; NSString * filepath = [docPath stringByAppendingPathComponent: @ "/aa. plist "]; // store the array to the plist file [arr writeToFile: filepath atomically: YES]; // read from the file path as array NSArray * arr2 = [NSArray arrayWithContentsOfFile: filepath];
NSDictionary is stored in Cache:
NSDictionary * dic =@ {@ "name": @ "lean", @ "age": @ 24, @ "height": @ 172 }; NSArray * dicArr = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSAllDomainsMask, YES); NSLog (@ "% @", dicArr [0]); NSString * dirPath = dicArr [0]; NSString * filePath = [dirPath stringByAppendingPathComponent: @ "dic. plist "]; // store Dictionary to plist file [dic writeToFile: filePath atomically: YES]; // read from file path as Dictionary NSDictionary * dic2 = [NSDictionary dictionaryWithContentsOfFile: filePath];
NSData image reading:
// Read/write images can be stored directly only through NSData. // The following example shows NSArray * arr = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSAllDomainsMask, YES); NSLog (@ "% @", arr [0]); NSString * cachePath = arr [0]; NSString * filePath = [cachePath stringByAppendingPathComponent: @ "image. plist "]; UIImage * image = [self. a image]; NSData * data = UIImageJPEGRepresentation (image, 1); [data writeToFile: filePath atomically: YES]; NSData * data2 = [NSData dataWithContentsOfFile: filePath]; UIImage * image2 = [UIImage imageWithData: data2]; self. b. image = image2;
Ios programming: Can I write logical operators in plist files? plist files only store some data.
IOS development: Plist file read/write. 1. Read and Write Data to plist; 2. Create a UITab instance based on the content of the plist node. // obtain the plist address.
NSString * plistPath = [[NSBundle mainBundle] pathForResource: @ "UpgradeDataArray" ofType: @ "plist"];
NSMutableDictionary * dataDic = [[NSMutableDictionary alloc] initWithContentsOfFile: plistPath]; // retrieve the corresponding data based on the storage type
// Add item content
[DataDic setObject: arr forKey: @ "upgradeData"];
// Obtain the sandbox Documents directory of the application
NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
NSString * plistPath1 = [paths objectAtIndex: 0];
// Obtain the complete file name
NSString * filename = [plistPath1 stringByAppendingPathComponent: @ "UpgradeDataArray. plist"];
// Input and write
BOOL fl = [dataDic writeToFile: filename atomically: YES]; // write
Easy to answer and adopt