zoukankan      html  css  js  c++  java
  • plist文件的 偏好设置 存储与读取 自定义对象归档

    plist文件的存储读取

     1 #import "HMViewController.h"
     2 
     3 @interface HMViewController ()
     4 
     5 @end
     6 
     7 @implementation HMViewController
     8 // 当点点击保存的时候调用
     9 - (IBAction)save:(id)sender {
    10     // 获取沙盒的根路径
    11 //    NSString *home = NSHomeDirectory();
    12     
    13     // 拼接Documents路径
    14 //    NSString *docPath = [home stringByAppendingString:@"/Documents"];
    15 //    NSString *docPath = [home stringByAppendingPathComponent:@"Documents"];
    16     
    17     
    18     /**
    19      *  NSDocumentDirectory : 查找Documents文件夹
    20         NSUserDomainMask : 在用户的应用程序下查找
    21      YES 把路径展开 NO 当前应用的根路径 == ~ 
    22      NO  ~/Documents
    23 
    24      */
    25     NSString *docPath =  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    26     NSLog(@"%@",docPath);
    27     
    28     // 拼接文件路径
    29     NSString *filePath = [docPath stringByAppendingPathComponent:@"data.plist"];
    30     
    31     // 只有具备writeToFile:的对象才能使用plist存储,NSArray
    32     NSArray *array = @[@1,@2,@"123"];
    33     
    34     [array writeToFile:filePath atomically:YES];
    35     
    36 //    NSLog(@"%@",docPath);
    37     
    38     
    39     
    40 }
    41 
    42 //当点击读取的时候调用
    43 - (IBAction)read:(id)sender {
    44     NSString *docPath =  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    45     NSLog(@"%@",docPath);
    46     
    47     // 拼接文件路径
    48     NSString *filePath = [docPath stringByAppendingPathComponent:@"data.plist"];
    49     
    50    NSArray *data = [NSArray arrayWithContentsOfFile:filePath];
    51     NSLog(@"%@",data);
    52     
    53 }
    54 
    55 - (void)viewDidLoad
    56 {
    57     [super viewDidLoad];
    58     // Do any additional setup after loading the view, typically from a nib.
    59 }
    60 
    61 - (void)didReceiveMemoryWarning
    62 {
    63     [super didReceiveMemoryWarning];
    64     // Dispose of any resources that can be recreated.
    65 }
    66 
    67 @end

    偏好设置的存储与读取

     1 #import "HMViewController.h"
     2 
     3 @interface HMViewController ()
     4 
     5 @end
     6 
     7 @implementation HMViewController
     8 - (IBAction)save:(id)sender {
     9     
    10     // [NSUserDefaults standardUserDefaults]可以直接操作偏好设置文件夹
    11     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    12     
    13     // 自动帮我们生成一个plist文件存放在偏好设置的文件夹
    14     [defaults setObject:@"hm" forKey:@"account"];
    15     
    16     // 同步:把内存中的数据和沙盒同步
    17     [defaults synchronize];
    18     
    19 }
    20 - (IBAction)read:(id)sender {
    21     // [NSUserDefaults standardUserDefaults]可以直接操作偏好设置文件夹
    22     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    23 
    24    NSLog(@"%@",[defaults objectForKey:@"account"]);
    25     
    26 //    NSDictionary *dict = @{@"account":@"hm"};
    27 //    
    28 //    dict writeToFile:<#(NSString *)#> atomically:<#(BOOL)#>
    29     
    30 }
    31 
    32 /*
    33     偏好设置的好处
    34     不用关心文件名
    35     快速进行键值对存储的时候用偏好设置
    36  
    37  
    38  */
    39 
    40 - (void)viewDidLoad
    41 {
    42     [super viewDidLoad];
    43     // Do any additional setup after loading the view, typically from a nib.
    44 }
    45 
    46 - (void)didReceiveMemoryWarning
    47 {
    48     [super didReceiveMemoryWarning];
    49     // Dispose of any resources that can be recreated.
    50 }
    51 
    52 @end

    自定义对象归解档

      1 //
      2 //  HMPerson.h
      3 //  08-自定义对象归档
      4 //
      5 //  Created by yz on 14-8-29.
      6 //  Copyright (c) 2014年 iThinker. All rights reserved.
      7 //
      8 
      9 #import <Foundation/Foundation.h>
     10 
     11 @interface HMPerson : NSObject<NSCoding>
     12 
     13 @property (nonatomic, assign) int age;
     14 
     15 @end
     16 
     17 //
     18 //  HMPerson.m
     19 //  08-自定义对象归档
     20 //
     21 //  Created by yz on 14-8-29.
     22 //  Copyright (c) 2014年 iThinker. All rights reserved.
     23 //
     24 
     25 #import "HMPerson.h"
     26 
     27 @implementation HMPerson
     28 
     29 // 在对象归档的时候调用
     30 // 哪些属性需要归档
     31 // 这些属性怎么归档
     32 - (void)encodeWithCoder:(NSCoder *)aCoder
     33 {
     34     [aCoder encodeInt:_age forKey:@"age"];
     35 }
     36 
     37 // 在对象解档的时候调用
     38 // 哪些属性需要解档
     39 // 这些属性怎么解档
     40 
     41 - (id)initWithCoder:(NSCoder *)aDecoder
     42 {
     43     // 当父类实现了NSCoding,就能调用 [super initWithCoder]
     44     if (self = [super init]) {
     45         _age = [aDecoder decodeIntForKey:@"age"];
     46     }
     47     return self;
     48 }
     49 
     50 @end
     51 
     52 //
     53 //  HMViewController.m
     54 //  08-自定义对象归档
     55 //
     56 //  Created by yz on 14-8-29.
     57 //  Copyright (c) 2014年 iThinker. All rights reserved.
     58 //
     59 
     60 #import "HMViewController.h"
     61 
     62 #import "HMPerson.h"
     63 
     64 @interface HMViewController ()
     65 
     66 @end
     67 
     68 @implementation HMViewController
     69 
     70 - (IBAction)save:(id)sender {
     71     HMPerson *person = [HMPerson new];
     72     person.age = 18;
     73     
     74     NSString *docPath =  NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
     75     
     76     // 拼接文件路径
     77     NSString *filePath = [docPath stringByAppendingPathComponent:@"person.data"];
     78     
     79     [NSKeyedArchiver archiveRootObject:person toFile:filePath];
     80     
     81 
     82 }
     83 - (IBAction)read:(id)sender {
     84     NSString *docPath =  NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
     85     
     86     // 拼接文件路径
     87     NSString *filePath = [docPath stringByAppendingPathComponent:@"person.data"];
     88     
     89     HMPerson *p = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
     90     NSLog(@"%d",p.age);
     91 }
     92 
     93 - (void)viewDidLoad
     94 {
     95     [super viewDidLoad];
     96     // Do any additional setup after loading the view, typically from a nib.
     97 }
     98 
     99 - (void)didReceiveMemoryWarning
    100 {
    101     [super didReceiveMemoryWarning];
    102     // Dispose of any resources that can be recreated.
    103 }
    104 
    105 @end
  • 相关阅读:
    x01.Weiqi.6: 立体棋子
    x01.Game.MapEditor: 地图编辑器
    SQL Server插入中文出现乱码??的解决办法
    Cachecontrol使用:header('Cachecontrol:private')
    验证身份证号码Javascript代码
    .Net GDI+缩放绘图
    Access(JETSQL)问题集锦
    Android开发中Google谷歌地图坐标系怎么转Baidu百度地图坐标系
    21、桥接模式(Bridge模式)详解
    22、装饰模式(装饰设计模式)详解
  • 原文地址:https://www.cnblogs.com/liqiantu/p/4441910.html
Copyright © 2011-2022 走看看