iOS数据持久化

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

   self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

   // Override point for customization after application launch.

   self.window.backgroundColor = [UIColor whiteColor];

   [self.window makeKeyAndVisible];

   //直接获取start这个key对应的值,如果获取到说明程序已经启动过了,如果没有获取到说明是第一次启动

   NSString *result = [[NSUserDefaults standardUserDefaults] objectForKey:@"start"];

   if ([result isEqualToString: @"启动过了"]) {

      //不是第一次启动

      NSLog(@"不是第一次"); 

   } else {

      //是第一次启动

      NSLog(@"第一次");

      [[NSUserDefaults standardUserDefaults] setObject:@"启动过了" forKey:@"start"];

   }

   RootViewController *root =[[RootViewController alloc] init];

   UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController: root];

   nav.navigationBar.barTintColor = [UIColor purpleColor];

   nav.navigationBar.translucent = NO;

   self.window.rootViewController = nav;

   return YES;

}

//*******************************************************************************************//

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end

//*****************************************************************************************//

#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {

    [super viewDidLoad];

   //获取沙盒的根路径

   NSLog(@"%@",NSHomeDirectory());

   //获取documents文件夹路径

   NSString *douPath =  [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

   NSLog(@"%@",douPath);

   //获取缓存文件夹路径

   NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];

   NSLog(@"%@",cachePath);

   //获取临时文件夹路径

   NSString *tmpPath =  NSTemporaryDirectory();

   NSLog(@"%@",tmpPath);

   //1NSUserDefaults

   [[NSUserDefaults standardUserDefaults] setObject:@"看着快播流着泪" forKey:@"kuaibo"];

   //

   NSString *result = [[NSUserDefaults standardUserDefaults] objectForKey:@"kuaibo"];

   NSLog(@"%@",result);

   //借助NSUserDefaults实现判断程序是不是第一次启动

   //docouments文件夹里创建一个金瓶梅.txt文件,并写入一句话

   //1、创建文件的路径

   NSString *filePath = [douPath stringByAppendingPathComponent:@"金瓶梅.txt"];

   //2、创建文件管理者

   NSFileManager *manager = [NSFileManager defaultManager];

   //判断文件是否存在,如果存在写入,如果不存在先创建文件

   if ([manager fileExistsAtPath: filePath]) {

      //文件存在

   } else {

      //文件不存在

      [manager createFileAtPath:filePath contents:nil attributes:nil];

   }

   //写入内容

   NSString *content = @"这是一个新的章节";

   [content writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];

   //读出来

   NSString *readResult = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error: nil];

   NSLog(@"%@",readResult);   

   //文件的复制(documents文件夹里面复制一份金瓶梅.txt)

   NSString *copyFilePath = [douPath stringByAppendingPathComponent:@"金瓶梅复制版.txt"];

   [manager copyItemAtPath:filePath toPath:copyFilePath error: nil];

   //文件的移动(将金瓶梅.txt移动到Library文件夹中)

   //获取Library文件夹路径

   NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)lastObject];

   NSString *moveFilePath = [libraryPath stringByAppendingPathComponent: @"金瓶梅.txt"];

   [manager moveItemAtPath:filePath toPath:moveFilePath error:nil];

   // 文件的删除(将金瓶梅复制版.txt删除)

   if ([manager fileExistsAtPath: copyFilePath]) {

      // 删除

      [manager removeItemAtPath:copyFilePath error: nil];

   }

   //查看文件夹下的所有文件

   NSArray *array = [manager contentsOfDirectoryAtPath:douPath error: nil];

   NSLog(@"%ld",[array count]);

   NSLog(@"%@",array);

   //总结

   //对文件的操作实际上是对文件路径的操作,需要借助文件管理者NSFileManager

   //读写文件只能是NSString,NSArray,NSDictionary,NSDtata,自定义类是无法直接写入文件的

   //目前开发过程中主流的文件处理NSUserDefaults,数据库,coredata

   //要把一个自定义的类写入文件,需要借助归档操作,读出来是反归档

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值