【字典数组NSDictionary和NSMutableDictionary的持久化 Objective-C语言】

一、NSMutableDictionary字典数组的持久化

1.例如,我这儿有一个字典数组

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@“jack”,@“name”,@“18”,@“age”,nil];

[dict setObject:@“广州市XX街道” forKey:@“address”];

[dict setObject:@“171.1” forKey:@“height”];

这里面是不是有4对数据啊,

同样的原理啊,这4对数据,在程序运行的时候,是不是存在内存里面去了啊,

我想把它存到硬盘里面,怎么办,

我们之前NSArray,是不是可以把NSArray数组的信息,存到plist文件里面去啊,

那咱么这个Dictionary能不能也存到plist文件里面去呢,可以,一样的原理啊,

Dictionary有个方法,叫做writeToFile

[dict writeToFile:(nonnull NSString *) atomically:(BOOL)];

好,给一个路径,@“/Users/Apple/Desktop/dict.plist”

记住,文件后缀名,需要是plist,

atomically,来个NO,不使用临时文件先存,如果成功后,再移动到指定目录,

好,打印一句话吧,NSLog(@“存储成功”);

输出:存储成功

打开dict.plist文件看一下

好,各位看清楚啊,Root,根,Dictionary,

这个时候,我整个plist文件保存的是什么,Type,是Dictionary,是不是是个字典啊,

这个字典里面是不是有键值对啊,有几个键值对啊,4 items,4个

有哪4个,展开:

address : 广州市XX街道

age : 18

height : 171.1

name : jack

第一个,键是什么,address,值是什么,广州市XX街道,这个值是什么类型的,String类型的,

这个时候,我就把NSDictionary中的数据,存起来了,

二、存进去之后,我把它读回来,还原回来,能不能还原回来

1.可以,用dictionaryWithContentsOfFile,这个方法,这是一个类方法,

[NSDictionary dictionaryWithContentsOfFile:(nonnull NSString *)];

好,我用一个NSDictionary类型的指针接一下,

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:@“/Users/Apple/Desktop/dict.plist”];

NSLog(@“%@”,dict);

输出:address = “\U5e7f\U5dde\U5e02XX\U63a5\U5230”;

age = 18;

height = “171.1”;

name = jack;

所以,我们也可以将这个字典数组的信息,给它持久化起来,

第一个是writeToFile,这个方法的签名如下,

- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;

这个方法,是将字典数组的信息,保存到plist文件中,

第二个是,从plist文件中,还原会字典,用dictionaryWithContentsOfFile,这个方法的签名如下,

+ (nullable NSDictionary < KeyType, ObjectType> *)dictionaryWithContentsOfFile:(NSString *)path;

好了,这就是我们这个字典数组的持久化

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是用Objective-C纯代码实现的签到日期界面。实现的界面包括一个导航栏、一个显示当前日期的标签和一个签到按钮。 ViewController.h文件: ```objective-c #import <UIKit/UIKit.h> @interface ViewController : UIViewController @end ``` ViewController.m文件: ```objective-c #import "ViewController.h" @interface ViewController () @property (nonatomic, strong) UILabel *dateLabel; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // 设置导航栏标题 self.navigationItem.title = @"签到"; // 设置背景颜色 self.view.backgroundColor = [UIColor whiteColor]; // 创建并添加日期标签 self.dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 50)]; self.dateLabel.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2 - 50); self.dateLabel.textAlignment = NSTextAlignmentCenter; self.dateLabel.textColor = [UIColor blackColor]; [self.view addSubview:self.dateLabel]; // 更新日期标签的文本 [self updateDateLabel]; // 创建并添加签到按钮 UIButton *checkInButton = [UIButton buttonWithType:UIButtonTypeSystem]; checkInButton.frame = CGRectMake(0, 0, 200, 50); checkInButton.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2 + 50); [checkInButton setTitle:@"签到" forState:UIControlStateNormal]; [checkInButton addTarget:self action:@selector(checkInButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:checkInButton]; } // 更新日期标签的文本 - (void)updateDateLabel { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyy年MM月dd日"; self.dateLabel.text = [formatter stringFromDate:[NSDate date]]; } // 签到按钮点击事件 - (void)checkInButtonClicked:(UIButton *)sender { // TODO: 处理签到逻辑 // 更新日期标签的文本 [self updateDateLabel]; } @end ``` 在AppDelegate.m文件中,将ViewController设置为根视图控制器: ```objective-c #import "AppDelegate.h" #import "ViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; ViewController *viewController = [[ViewController alloc] init]; UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController]; self.window.rootViewController = navigationController; [self.window makeKeyAndVisible]; return YES; } ``` 这样就完成了一个简单的签到日期界面的实现。运行程序后,可以看到一个带有日期标签和签到按钮的界面。点击签到按钮后,日期标签上的日期会更新为当前日期。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清风清晨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值