iOS——plist的创建,数据写入与读取

iOS中plist的创建,数据写入与读取
Documents:应用将数据存储在Documents中,但基于NSuserDefaults的首选项设置除外Library:基于NSUserDefaults的首选项设置存储在Library/Preferences文件夹中

09/02/201

              

 

         tmp:供应用存储临时文件。iOS设备执行同步时候,iTunes不会备份tmp中的文件,但在不需要这些文件时,应用会负责删除tmp中的文件,以免占用文件系统的空间。

 

#import "ViewController.h"

@interface ViewController ()

@property(strong,nonatomic)IBOutletCollection(UITextField)NSArray *lineFields;

@end

@implementation ViewController

-(NSString *)dataFilePath{
    
    //查找Document目录并在其后附加数据文件的文件名,这样就得到了数据文件的完整的路径

    NSArray *paths = NSSearchPathForDirectoriesInDomains(
                    NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [paths objectAtIndex:0];
    
    return [documentDirectory stringByAppendingPathComponent:@"data.plist"];


}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //
    NSString *filePath = [self dataFilePath];
    
    //检查数据文件在不在
    
    if ([[NSFileManager defaultManager]fileExistsAtPath:filePath]) {
        
        NSArray *array = [[NSArray alloc]initWithContentsOfFile:filePath];
        for (int i = 0; i < 4; i++) {
            
            UITextField *theFiled = self.lineFields[i];
            theFiled.text = array[i];
            
        }
    }
    
    UIApplication *app = [UIApplication sharedApplication];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app];
    
}
//应用在终止运行或者进去后台之前保存数据

-(void)applicationWillResignActive:(NSNotification *)notification{

    NSString *filePath = [self dataFilePath];
    //创建一个数组 ,并读取文件内容
    NSArray *array = [self.lineFields valueForKey:@"text"];
    [array writeToFile:filePath atomically:YES];
}

-------------------------------------------------------------------------------------------------------------------------------

参考代码:

plist 文件读写
    //1. 查找Document目录
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths    objectAtIndex:0];
    NSLog(@"path = %@",path);

//得到了数据文件的完整的路径
    NSString *filename=[path stringByAppendingPathComponent:@"test.plist"];    
    

//检测数据文件是否存在

   NSFileManager* fm = [NSFileManager defaultManager];
    [fm createFileAtPath:filename contents:nil attributes:nil];        
    //NSDictionary* dic = [NSDictionary dictionaryWithContentsOfFile:plistPath];
    
    //创建一个dic,写到plist文件里
    NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:@"sina",@"1",@"163",@"2",nil];
    [dic writeToFile:filename atomically:YES];
    
    //读文件
    NSDictionary* dic2 = [NSDictionary dictionaryWithContentsOfFile:filename];

    NSLog(@"dic is:%@",dic2);

 属性列表序列化:

序列化对象是指可以被转换为字节流以便于存储到文件中或通过网络进行传输的对象。

虽然说任何对象都可以被序列化,但只有某些对象才能被放置到某个集合类中(NSDictionary,NSArray),然后才使用该集合类的writeToFile:atomically:方法或writeToURL:atomically:方法将它们存储到属性列表中。

属性列表的对象有:

NSArray;

NSMutableArray

NSDictionary

NSMutableDictionary

NSData

NSMutableData

NSString

NSMutableString

NSNumber

NSDate

属性列表方法的一个问题就是:无法将自定义对象序列化到属性列表中,另外也不能使用其他类,例如:NSURL,UIImage,UIColor等。

 

原文地址:https://www.cnblogs.com/linxiu-0925/p/5070947.html