年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2255|回复: 1

第十三讲:Objective-C基本数据类型之NSDictionary

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

    [LV.9]以坛为家II

    发表于 2013-12-22 19:53:01 | 显示全部楼层 |阅读模式
    本帖最后由 Sian 于 2013-12-22 21:37 编辑
    1. #import <Foundation/Foundation.h>
    2. int main(int argc, const char * argv[])
    3. {
    4.         @autoreleasepool{
    5.                 NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"One",@"1",@"Two",@"2",@"Three",@"3",nil];
    6.                
    7.                 NSLog(@"%@",dict);
    8.                
    9.                 NSEumerator * eumerator = [dict objectEumerator];
    10.                 id obj;
    11.                 while(obj = [eumerator nextObject]){
    12.                         NSLog(@"%@",obj);
    13.                 }//枚举器法
    14.                
    15.                  for(id obj in dict){
    16.                         NSLog(@"%@",obj);
    17.                         NSLog(@"%@",[dict objectForKey:obj]);
    18.                  }
    19.                 [dict release];
    20.         }
    21. }
    复制代码
    可变字典
    1. #import <Foundation/Foundation.h>
    2. int main(int argc, const char * argv[])
    3. {
    4.         @autoreleasepool{
    5.                 NSMutableDictionay * dict = [[NSMutableDictionay alloc] init];
    6.                
    7.                 [dict setObject:@"One" forKey:@"1"];//增加一个键值对
    8.                 [dict setObject"@"Two" forKey:@"2"];
    9.                
    10.                 NSLog(@"%@",dict);
    11.                
    12.                 [dict removeObjectForKey:@"1"];//删除一个键值对
    13.                
    14.                 NSLog(@"%@",dict);
    15.                
    16.                 [dict release];
    17.         }
    18. }
    复制代码


  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

     楼主| 发表于 2014-1-10 21:32:30 | 显示全部楼层
    可变字典可删除其中的某个键值对,可以根据键来索引:
    1. //
    2. //  main.m
    3. //  NSDictionary
    4. //
    5. //  Created by yusian on 14-1-10.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import <Foundation/Foundation.h>
    9. int main(int argc, const char * argv[])
    10. {
    11.     @autoreleasepool{
    12.         
    13.         NSMutableDictionary * dict = [[NSMutableDictionary alloc] init];
    14.         
    15.         [dict setObject:@"One" forKey:@"1"];
    16.         [dict setObject:@"Two" forKey:@"2"];
    17.         
    18.         NSLog(@"%@",dict);
    19.         
    20.         [dict removeObjectForKey:@"1"];
    21.         
    22.         NSLog(@"%@",dict);
    23.         
    24.         [dict release];
    25.         
    26.     }
    27. }
    复制代码
    运行结果:
    1. 2014-01-10 21:32:22.505 NSDictionary[1486:303] {
    2.     1 = One;
    3.     2 = Two;
    4. }
    5. 2014-01-10 21:32:22.506 NSDictionary[1486:303] {
    6.     2 = Two;
    7. }
    8. Program ended with exit code: 0
    复制代码


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

    本版积分规则

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

    GMT+8, 2024-5-2 11:38 , Processed in 0.047934 second(s), 18 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

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