年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1889|回复: 0

NSDictionary的基本使用

[复制链接]
  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

    发表于 2014-3-23 22:20:22 | 显示全部楼层 |阅读模式
    1. //
    2. //  main.m
    3. //  NSDictionary
    4. //
    5. //  Created by yusian on 14-3-23.
    6. //  Copyright (c) 2014年 小龙虾论坛. All rights reserved.
    7. //
    8. #import <Foundation/Foundation.h>
    9. int main()
    10. {
    11.     // 字典的几种赋值方式:
    12.     // 1、直接使用类名方法初始化,内容为空
    13.     // NSDictionary * dict = [NSDictionary dictionary];
    14.    
    15.     // 2、调用类方法dictionaryWithObject:
    16.     // NSDictionary *dict = [NSDictionary dictionaryWithObject:@"yusian" forKey:@"name"];
    17.    
    18.     // 3、调用类方法传数组分别做为Objects与Keys
    19.     // NSArray *objs = @[@"yusian", @"27"];
    20.     // NSArray *keys = @[@"name", @"age"];
    21.     // NSDictionary *dict = [NSDictionary dictionaryWithObjects:objs forKeys:keys];
    22.    
    23.     // 4、使用类方法同时传键值对
    24.     /* NSDictionary * dict = [NSDictionary dictionaryWithObjectsAndKeys:
    25.                            @"yusian", @"name",
    26.                            @"27", @"age", nil];*/
    27.    
    28.     // 5、使用编译器特性
    29.     NSDictionary *dict = @{@"name" : @"yusian", @"age" : @"27"};
    30.    
    31.     // 6、输出整个字典
    32.     // NSLog(@"%@", dict);
    33.    
    34.     // 7、输出字典中的某个值
    35.     // 通过方法取
    36.     // id obj = [dict objectForKey:@"name"];
    37.    
    38.     // 通过编译器特性,类似数组的取值方式实现
    39.     id obj = dict[@"name"];
    40.    
    41.     NSLog(@"%@", obj);
    42.    
    43.     return 0;
    44. }
    复制代码


    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-5-5 06:57 , Processed in 0.043577 second(s), 19 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表