Objective-C语法之NSDictionary和NSMutableDictionary

Java有Map,可以把数据以键值对的形式储存起来,取值的时候通过key就可以直接拿到对应的值,方便快捷。在Objective-C语言中,词典就是做这样的事情的,和NSArray一样,一个词典对象也能保存不同类型的值,词典也分别有不可变词典和可变的词典(NSDictionary与NSMutableDictionary),前者是线程安全的,后者不是 。


1、不可变词典NSDictionary的主要用法:

[NSDictionary dictionaryWithObjectsAndKeys:..] : 使用键值对直接创建词典对象,结尾必需使用nil标志结束。

[dictionary count]: 得到词典的键值对数量。
[dictionary keyEnumerator]: 将词典的所有key储存在NSEnumerator中,类似于Java语言中的迭代器
[dictionary objectEnumerator]: 将词典的所有value储存在NSEnumerator中
[dictionary objectForKey:key]: 通过传入key对象可以拿到当前key对应储存的值。

代码示例:

[cpp]  view plain copy
  1. int main(int argc, const char * argv[])  
  2. {  
  3.     @autoreleasepool {  
  4.         NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"25",@"age",@"张三",@"name",@"男",@"性别",nil];  
  5.         NSLog(@"%lu", [dictionary count]);  
  6.         NSEnumerator *enumeratorKey = [dictionary keyEnumerator];  
  7.         for (NSObject *object in enumeratorKey) {  
  8.             NSLog(@"key:%@", object);  
  9.         }  
  10.           
  11.         NSEnumerator *enumeratorObject = [dictionary objectEnumerator];  
  12.         for (NSObject *object in enumeratorObject) {  
  13.             NSLog(@"value:%@", object);  
  14.         }  
  15.         NSLog(@"key name的值是:%@", [dictionary objectForKey:@"name"]);  
  16.           
  17.     }  
  18.     return 0;  
  19. }  

打印结果;

[objc]  view plain copy
  1. 2012-07-09 16:31:18.276 objectiveC[2965:4033  
  2. 2012-07-09 16:31:18.282 objectiveC[2965:403] key:age  
  3. 2012-07-09 16:31:18.282 objectiveC[2965:403] key:name  
  4. 2012-07-09 16:31:18.283 objectiveC[2965:403] key:性别  
  5. 2012-07-09 16:31:18.283 objectiveC[2965:403] value:25  
  6. 2012-07-09 16:31:18.284 objectiveC[2965:403] value:张三  
  7. 2012-07-09 16:31:18.284 objectiveC[2965:403] value:男  
  8. 2012-07-09 16:31:18.285 objectiveC[2965:403] key name的值是:张三  

2、可变的词典NSMutableDictionary。

NSMutableDictionary是NSDictionary的子类,所以继承了NSDictionary的方法, 以上的代码对NSMutableDictionary来说完全可用。我们试试不一样的地方

增删键值数据。

[dictionary setObject: forKey:] :向可变的词典动态的添加数据 
[dictionary removeAllObjects..] : 删除掉词典中的所有数据。
[dictionary removeObjectForKey..] :删除掉词典中指定key的数据

代码示例:

[cpp]  view plain copy
  1. int main(int argc, const char * argv[])  
  2. {  
  3.     @autoreleasepool {  
  4.         NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"25",@"age",@"张三",@"name",@"男",@"性别",nil];  
  5.         [dictionary setObject:@"30名" forKey:@"名次"];  
  6.           
  7.         NSLog(@"%lu", [dictionary count]);  
  8.         NSEnumerator *enumeratorKey = [dictionary keyEnumerator];  
  9.         for (NSObject *object in enumeratorKey) {  
  10.             NSLog(@"key:%@", object);  
  11.         }  
  12.           
  13.         NSEnumerator *enumeratorObject = [dictionary objectEnumerator];  
  14.         for (NSObject *object in enumeratorObject) {  
  15.             NSLog(@"value:%@", object);  
  16.         }  
  17.         NSLog(@"key 名次的值是:%@", [dictionary objectForKey:@"名次"]);  
  18.         [dictionary removeObjectForKey:@"名词"];  
  19.         NSLog(@"%lu", [dictionary count]);  
  20.     }  
  21.     return 0;  
  22. }  

打印结果:

[objc]  view plain copy
  1. 2012-07-09 16:37:07.037 objectiveC[3053:4034  
  2. 2012-07-09 16:37:07.042 objectiveC[3053:403] key:age  
  3. 2012-07-09 16:37:07.043 objectiveC[3053:403] key:性别  
  4. 2012-07-09 16:37:07.043 objectiveC[3053:403] key:name  
  5. 2012-07-09 16:37:07.044 objectiveC[3053:403] key:名次  
  6. 2012-07-09 16:37:07.044 objectiveC[3053:403] value:25  
  7. 2012-07-09 16:37:07.045 objectiveC[3053:403] value:男  
  8. 2012-07-09 16:37:07.045 objectiveC[3053:403] value:张三  
  9. 2012-07-09 16:37:07.046 objectiveC[3053:403] value:30名  
  10. 2012-07-09 16:37:07.046 objectiveC[3053:403] key 名次的值是:30名  
  • 0
    点赞
  • 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值