Objective-C个人学习系列(3) NSDictionary NSMutableDictionary

#import <Foundation/Foundation.h>

 

int main(int argc, const char * argv[])

{

 

    @autoreleasepool {

        //--------------------------NSDictionary-----------------------

        //通过键-值对创建字典,先创建对象实例,再初始化对象

        //- (instancetype)initWithObjectsAndKeys:(id)firstObject, , ...

        NSDictionary * dic = [[NSDictionaryalloc] initWithObjectsAndKeys:

                              @"zhangsan",@"001",@"wangwu",@"002", nil];

        //根据key返回对象值

        NSString * s = [dic objectForKey:@"001"];

        NSLog(@"%@",s);

        //遍历字典,遍历出key,通过key得到object

        for (NSString* key in dic) {

            NSString* value = [dic objectForKey:key];

            NSLog(@"%@",value);

        }

        

        NSArray *arrKey = [[NSArray alloc]initWithObjects:@"101",@"102",@"103",@"104", nil];

        NSArray *arrValue = [[NSArray alloc]initWithObjects:@"张三",@"李四",@"王五",@"赵六", nil];

        

        //通过现有的对象数组创建字典对象

        //- (instancetype)initWithObjects:(NSArray *)objects forKeys:(NSArray *)keys

        NSDictionary* dic2 = [[NSDictionary alloc]initWithObjects:arrValue

                                                          forKeys:arrKey];

        for (NSString* key in dic2) {

            NSString* value = [dic2 objectForKey:key];

            NSLog(@"%@",value);

        }

        

        //通过类方法直接创建字典对象

        NSDictionary *dd = [NSDictionary dictionaryWithObjects:arrValue

                                                       forKeys:arrKey];

        for (NSString* key in dd) {

            NSString* value = [dd objectForKey:key];

            NSLog(@"%@",value);

        }

        

        

        

        //字典比较

        //- (BOOL)isEqualToDictionary:(NSDictionary *)otherDictionary

        //字典中内容相等返回YES,否则返回NO

        BOOL isEqual = [dd isEqualToDictionary:dic];

        NSLog(@"%hhd",isEqual);             //Output:0

        BOOL isEqual2 = [dd isEqualToDictionary:dic2];

        NSLog(@"%hhd",isEqual2);             //Output:1

        

        

        //--------------------------NSMutableDictionary-----------------------

        //创建可变字典

        NSMutableDictionary* mDic = [[NSMutableDictionaryalloc] init] ;

        

        NSNumber* num01 = [[NSNumber alloc] initWithInt:1001] ;

        NSNumber* num02 = [[NSNumber alloc] initWithInt:1002] ;

        NSNumber* num03 = [[NSNumber alloc] initWithInt:1003] ;

        NSNumber* num04 = [[NSNumber alloc] initWithInt:1004] ;

        NSNumber* num05 = [[NSNumber alloc] initWithInt:1005] ;

        

        NSString* name01 = @"小刚";

        NSString* name02 = @"小明";

        NSString* name03 = @"小强";

        NSString* name04 = @"小红";

        NSString* name05 = @"小霞";

        //添加键值对

        [mDic setObject:name01 forKey:num01];

        [mDic setObject:name02 forKey:num02] ;

        [mDic setObject:name03 forKey:num03] ;

        [mDic setObject:name04 forKey:num04] ;

        [mDic setObject:name05 forKey:num05] ;

        //如果添加的新元素的key与原来的字典中的key一样

        //将原来key所对应的值覆盖掉

        //不会添加新元素空间

        [mDic setObject:name05 forKey:num01] ;

        

        for (NSNumber* num in mDic)

        {

            NSLog(@"name = %@", [mDic objectForKey:num]);

        }

 

        //删除所有元素

        //[mDic removeAllObjects] ;

        //根据key删除一个元素

        [mDic removeObjectForKey:num03] ;

        [mDic removeObjectForKey:num02] ;

        [mDic removeObjectForKey:num02] ;

        

        for (NSNumber* num in mDic)

        {

            NSLog(@"name = %@", [mDic objectForKey:num]);

        }

 

        //创建删除关键字数组,(根据多个key删除多个元素)

        NSArray* arrayDel = [NSArray arrayWithObjects:num04,num05, nil];

        //根据关键字数组删除元素

        [mDic removeObjectsForKeys:arrayDel] ;

        NSLog(@"\n");

        for (NSNumber* num in mDic)

        {

            NSLog(@"name = %@", [mDic objectForKey:num]);

        }

 

    }

}

 

 

更多内容参考官方文档:https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/Reference/Reference.html#//apple_ref/doc/uid/20000140-SW11

  • 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、付费专栏及课程。

余额充值