zoukankan      html  css  js  c++  java
  • iphone开发中的数据存储:Property lists

    定义两个方法:

    - (NSString *)dataFilePath;获取返回数据文件的完整路径名

    - (void)applicationWillResignActive:(NSNotification *)notification;保存数据,参数notification的作用是在对象之间传递通知,保持通信。

    #define kFilename        @"data.plist"


    - (NSString *)dataFilePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(
    NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:kFilename];
    }

     

    - (void)applicationWillResignActive:(NSNotification *)notification {
    NSMutableArray *array = [[NSMutableArray alloc] init];
    [array addObject:field1.text];
    [array addObject:field2.text];
    [array addObject:field3.text];
    [array addObject:field4.text];
    [array writeToFile:[self dataFilePath] atomically:YES];
    }

    (例子程序为保存四个textField的字符)

    之后在viewDidLoad中添加代码:

    - (void)viewDidLoad
    {
    [super viewDidLoad];

    NSString *filePath = [self dataFilePath];
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
    NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
    field1.text = [array objectAtIndex:0];
    field2.text = [array objectAtIndex:1];
    field3.text = [array objectAtIndex:2];
    field4.text = [array objectAtIndex:3];
    }

    UIApplication *app = [UIApplication sharedApplication];
    [[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(applicationWillResignActive:)
    name:UIApplicationWillResignActiveNotification
    object:app];
    }




  • 相关阅读:
    laravel 安装完成后安装 vendor 目录
    requires php ~7.1 -> your PHP version (7.0.18) does not satisfy that requirement
    查看laravel版本
    git update-index --assume-unchanged
    Git 取消跟踪已版本控制的文件(亲测可行)
    git把某个文件去除版本控制
    git如何移除某文件夹的版本控制
    git如何移除某文件的版本控制
    git 教程
    Git branch && Git checkout常见用法
  • 原文地址:https://www.cnblogs.com/mybkn/p/2418996.html
Copyright © 2011-2022 走看看