首页 > 移动平台 > 详细

ios应用数据存储方式(偏好设置)-转

时间:2018-12-12 18:33:08      阅读:131      评论:0      收藏:0      [点我收藏+]

一.简单介绍 
1.很多ios应用都支持偏好设置,比如保存用户名,密码,字体大小等设置,ios提供了一套标准的解决方案来为应用加入偏好设置功能。 
2.每个应用都有个NSUserDefaults实例,通过它来存储偏好设置。比如,保存用户名,字体大小,是否自动登录。 
3.存储位置 
技术分享图片 
4.存储形式 
技术分享图片

 

二.代码示例

#import "ViewController.h"

#define CURRENT_SCREEN_WIDTH     [UIScreen mainScreen].bounds.size.width
#define CURRENT_SCREEN_HEIGHT     ([UIScreen mainScreen].bounds.size.height - 64)
#define BUTTON_WIDTH     80
#define BUTTON_HEIGHT    40

@interface ViewController ()

//保存数据按钮
@property(nonatomic,strong) UIButton *saveButton;
//读取数据按钮
@property(nonatomic,strong) UIButton *readButton;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initControl];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//初始化控件
- (void)initControl{
    _saveButton = [[UIButton alloc] initWithFrame:CGRectMake(CURRENT_SCREEN_WIDTH/2 - BUTTON_WIDTH/2,
                                                             CURRENT_SCREEN_HEIGHT/2 - BUTTON_HEIGHT,
                                                             BUTTON_WIDTH,
                                                             BUTTON_HEIGHT)];
    [_saveButton setTitle:@"保存数据" forState:UIControlStateNormal];
    [_saveButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_saveButton addTarget:self
                    action:@selector(saveClick)
          forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_saveButton];

    _readButton = [[UIButton alloc] initWithFrame:CGRectMake(CURRENT_SCREEN_WIDTH/2 - BUTTON_WIDTH/2,
                                                             _saveButton.frame.origin.y + _saveButton.frame.size.height + 60,
                                                             BUTTON_WIDTH,
                                                             BUTTON_HEIGHT)];
    [_readButton setTitle:@"读取数据" forState:UIControlStateNormal];
    [_readButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_readButton addTarget:self
                    action:@selector(readClick)
          forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_readButton];

}

#pragma mark -按钮事件
- (void)saveClick{
    //获取NSUserDefaults对象
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    //保存数据(如果设置数据之后没有同步,会在将来某一时间点自动将数据保存到Preferences文件夹下面)
    [defaults setObject:@"DuBo" forKey:@"name"];
    [defaults setInteger:31 forKey:@"age"];
    [defaults setDouble:1.76f forKey:@"height"];

    //强制让数据立刻保存
    [defaults synchronize];
}

- (void)readClick{
    //获取NSUserDefaults对象
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    //读取保存数据
    NSString *name = [defaults objectForKey:@"name"];
    NSInteger age = [defaults integerForKey:@"age"];
    double height = [defaults doubleForKey:@"height"];

    //打印数据
    NSLog(@"name = %@",name);
    NSLog(@"age = %d",(int)age);
    NSLog(@"height = %f",height);
}

@end

 

三.重要说明 
1.偏好设置是专门用来保存应用程序的配置信息的,一般情况不要在偏好设置中保存其他数据。如果利用系统的偏好设置来存储数据,默认就是存储在Preferences文件夹下面的,偏好设置会将所有的数据都保存到同一个文件中。 
2.使用偏好设置对数据进行保存之后,它保存到系统的时间是不确定的,会在将来某一时间点自动将数据保存到Preferences文件夹下面,如果需要即刻将数据存储,可以使用[defaults synchronize]。 
3.所有的信息都写在一个文件中,对比简单的plist可以保存和读取基本数据类型。 
4.获取NSUserDefaults,保存 (读取)数据。

ios应用数据存储方式(偏好设置)-转

原文:https://www.cnblogs.com/jiuyi/p/10109876.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!